Find Number Of Sundays Between Two Dates

Started by raghukumarabout 18 years ago4 messagesgeneral
Jump to latest
#1raghukumar
raghukumarn@gmail.com

Hai EverBody,

Can I know what is the query by which we can find the number of
sundays between two given dates in postgres

Thanks In Advance,
Raghu.......
--
View this message in context: http://www.nabble.com/Find-Number-Of-Sundays-Between-Two-Dates-tp15843956p15843956.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#2A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: raghukumar (#1)
Re: Find Number Of Sundays Between Two Dates

am Tue, dem 04.03.2008, um 22:16:07 -0800 mailte raghukumar folgendes:

Hai EverBody,

Can I know what is the query by which we can find the number of
sundays between two given dates in postgres

No problem, for instance for month january 2008:

test=*# select count(1) from
(select '2008-01-01'::date + s*'1day'::interval as datum from
generate_series(0,30) s)foo
where extract(dow from datum)=6;
count
-------
4
(1 row)

With generate_series() i generate a list of dates, and later i check if
the date are a saturday. Okay, you need to know sunday -> change from 6
to 0 and ou course, you can calculate the parameter for the
generate_series like

test=*# select count(1) from
(select '2008-01-01'::date + s*'1day'::interval as datum from
generate_series(0,'2008-01-31'::date - '2008-01-01'::date) s)foo
where extract(dow from datum)=0;
count
-------
4
(1 row)

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

#3Garry Saddington
garry@schoolteachers.co.uk
In reply to: raghukumar (#1)
Re: Find Number Of Sundays Between Two Dates

On Wednesday 05 March 2008 06:16, raghukumar wrote:

Hai EverBody,

Can I know what is the query by which we can find the number of
sundays between two given dates in postgres

Thanks In Advance,
Raghu.......

I think the easiest way is to set up a table populated with all dates and days
between two years that are important to your application and then just do
simple selects on that table.
Regards
Garry

#4Dawid Kuroczko
qnex42@gmail.com
In reply to: A. Kretschmer (#2)
Re: Find Number Of Sundays Between Two Dates

On 3/5/08, A. Kretschmer <andreas.kretschmer@schollglas.com> wrote:

With generate_series() i generate a list of dates, and later i check if
the date are a saturday. Okay, you need to know sunday -> change from 6
to 0 and ou course, you can calculate the parameter for the
generate_series like

I find it convenient to create a custom version of generate_series:

CREATE OR REPLACE FUNCTION generate_series(date,date)
RETURNS SETOF date AS $$
SELECT $1 + n FROM generate_series(0,$2-$1) AS x(n)
$$ LANGUAGE SQL IMMUTABLE;

Regards,
Dawid