Re: Last day of month - Mailing list pgsql-sql

From Greg Sabino Mullane
Subject Re: Last day of month
Date
Msg-id 542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com
Whole thread Raw
In response to Last day of month  ("Kumar" <sgnerd@yahoo.com.sg>)
Responses Re: Last day of month
Re: Last day of month
Re: Last day of month
List pgsql-sql
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> How to find the last sunday/mon..../sat of any given month.
There is probably a smoother way to do it, but here is a
quick little function to do what you ask. Feed it a date
and a number, where 0 is Sunday, 1 is Monday, etc.
CREATE OR REPLACE FUNCTION lastday(date,int) RETURNS DATE AS '
DECLARE match date; tomorrow date;
BEGIN
SELECT TO_DATE((SELECT EXTRACT(\'year\' FROM $1) || \' \' ||        EXTRACT(\'month\' FROM $1) || \' 01\'), \'YYYY MM
DD\')INTO tomorrow;
 
LOOP tomorrow := tomorrow + \'24 hours\'::interval; IF (1 = EXTRACT(\'day\' FROM tomorrow) ) THEN   RETURN match; END
IF;IF ($2 = EXTRACT(\'dow\' FROM tomorrow)) THEN   match := tomorrow; END IF;
 
END LOOP;
END;
' LANGUAGE plpgsql;
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200402252206
                                   
 
-----BEGIN PGP SIGNATURE-----
iD8DBQFAPWL1vJuQZxSWSsgRAu0tAKDO7oKbxOmfDpCUYpeDSwCwyALs7QCgvKT3
x+aqhBqzm9F87ESbsMe6HdQ=
=AriI
-----END PGP SIGNATURE-----




pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Scalar in a range (but textual not numeric)
Next
From: Kenneth Gonsalves
Date:
Subject: updating remote database