From PostgreSQL wiki
by Scott Bailey 'Artacus'
Often when working with dates, we either need to get the first day of the month or the last day of the month. Getting the first day is easy and can be done with date_trunc.
SELECT date_trunc('MONTH', dtCol)::DATE;
But getting the last day is not so straight forward. Users coming from Oracle will recognize this one.
CREATE OR REPLACE FUNCTION last_day(DATE) RETURNS DATE AS $$ SELECT (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1 day')::DATE; $$ LANGUAGE 'sql' IMMUTABLE STRICT;
This snippet was borrowed from the Chronos Temporal Extensions project. Chronos is a complete toolkit for working with temporal data in Postgres and Oracle. I'll post some of the more general functions here. (Since I'm the author, I guess that's alright.)