I am seeking to find first date of the month in the corresponding table:
So if i have 26/08/2011 August as date and 2 months to add, it becomes 26/10/2011. I need the first date of the resulting month- like 01/10/2011.
Can this be achieved in SQL?
Update : I could get the date of the month using
DATEADD(month,months_add, date)
Couldnt get to "beginning of month". Tried: How can I select the first day of a month in SQL?
But for me it throws the error: function pg_catalog.date_diff("unknown", integer, date) does not exist;
3
Answers
You could try using date_add for add two months and date_sub for sub the days -1
Assuming your date is currently a
varchar
indd/MM/yyyy
format, you can useSTR_TO_DATE
to convert it to aDATE
type column, then useDATE_ADD
with yourmonths_add
column to dynamically add months then finally useDATE_FORMAT
to display it back in a01/MM/yyyy
format with the first day of the month.Result:
Fiddle here.