-----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(at)turnstep(dot)com
PGP Key: 0x14964AC8 200402252206
-----BEGIN PGP SIGNATURE-----
iD8DBQFAPWL1vJuQZxSWSsgRAu0tAKDO7oKbxOmfDpCUYpeDSwCwyALs7QCgvKT3
x+aqhBqzm9F87ESbsMe6HdQ=
=AriI
-----END PGP SIGNATURE-----