PostgreSQL – How to Get the First Day of Month From a Date
To get the first day of the month from a date in PostgreSQL, you can use the DATE_TRUNC
function. Here’s an example:
SELECT DATE_TRUNC('month', your_date_column) AS first_day_of_month FROM your_table;
where your_date_column
is the name of your date column and your_table
is the name of your table.
This query will return the first day of the month for each date in the specified column. The DATE_TRUNC
function truncates the date to the beginning of the specified period, which in this case is ‘month’.
For example, if you have a table called sales
with a date column named transaction_date
, you can retrieve the first day of the month for each transaction like this:
SELECT transaction_date, DATE(DATE_TRUNC('month', transaction_date)) AS first_day_of_month FROM sales;
This will give you a result set with the first day of the month for each transaction date in the sales
table.