This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing aUser defined function to find Last Day of The Month in PostgreSQL.
Once in a month, One of our Postgres reporting servers requires maintenance like VACUUM FULL activity.
For us that one day would be the last day of the month because on the last day of the month we are calculating some additional information.
Here, I am sharing one UDF to find out the Last Day of the month which we can use for scheduling the maintenance.
Function script:
1 2 3 4 5 6 |
CREATE OR REPLACE FUNCTION fn_GetLastDayOfMonth(DATE) RETURNS DATE AS $$ SELECT (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1 day')::DATE; $$ LANGUAGE 'sql' IMMUTABLE STRICT; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
SELECT *FROM fn_GetLastDayOfMonth(NOW()::DATE); /* Result: fn_getlastdayofmonth ---------------------- 2016-10-31 */ SELECT *FROM fn_GetLastDayOfMonth('20161101'); /* Result: fn_getlastdayofmonth ---------------------- 2016-11-30 */ SELECT *FROM fn_GetLastDayOfMonth('20161008'); /* Result: fn_getlastdayofmonth ---------------------- 2016-10-31 */ |
Leave a Reply