pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
To: Zot O'Connor <zot@zotconsulting.com>
To: postgres sql <pgsql-sql@hub.org>
To: Herouth Maoz <herouth@oumail.openu.ac.il>
Subject: Re: [SQL] Week of year function?
Date: Mon, 25 Oct 1999 20:04:22 +0100
Message-ID: <002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk> (raw)
References: <l03130302b43a3e85e018@[147.233.159.109]>


----- Original Message -----
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Zot O'Connor <zot@zotconsulting.com>; postgres sql <pgsql-sql@hub.org>
Sent: Monday, October 25, 1999 6:14 PM
Subject: Re: [SQL] Week of year function?


> At 21:52 +0200 on 22/10/1999, Zot O'Connor wrote:
>
>
> > Is there a function to return the week of the year (0-51)?
>
> Seems you only need to divide the day of the year by seven to reach that,
> don't you?
>
> Maybe you should try:
>
> CREATE FUNCTION week( datetime ) RETURNS int4 AS '
>   SELECT int( date_part( ''day'', $1 - date_trunc( ''year'', $1 ) ) ) / 7
> ' LANGUAGE 'sql';

I don't think that's quite right. You would need to add some maths to make
sure that if January 1st is a Wednesday, week 1 of the year begins on
January 6th (with Monday as first day of week) or Jan 5th (Sunday as first
day of week).

CREATE FUNCTION week( datetime ) RETURNS int4 AS '
  SELECT (int( date_part( ''day'', $1 - date_trunc( ''year'',
$1 )))-datepart(''dow'',date_trunc(''year'',$1)))) / 7
 ' LANGUAGE 'sql';

I don't have postgres accessible from this computer, but I think it should
be something like that - gives weeks 0-51 if dow returns 0-6. Herouth can
probably rewrite it more concisely..

Yours,
Moray





view thread (7+ messages)  latest in thread

Message-ID: <002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk>
Permalink:  ../002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk/
Also on:    postgresql.org/message-id/002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: moray.mcconnachie@computing-services.oxford.ac.uk, zot@zotconsulting.com, pgsql-sql@hub.org, herouth@oumail.openu.ac.il
  Subject: Re: [SQL] Week of year function?
  In-Reply-To: <002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk>
  References: <l03130302b43a3e85e018@[147.233.159.109]> <002501bf1f1b$c03a7da0$681b4cc0@public.ox.ac.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox