Skip site navigation (1) Skip section navigation (2)

Re: inverse of "day of year"

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Creager <Robert_Creager(at)LogicalChaos(dot)org>
Cc: Martin Marques <martin(at)bugs(dot)unl(dot)edu(dot)ar>, pgsql-sql(at)postgresql(dot)org
Subject: Re: inverse of "day of year"
Date: 2004-03-19 15:10:05
Message-ID: 24189.1079709005@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-sql
Robert Creager <Robert_Creager(at)LogicalChaos(dot)org> writes:
> Martin Marques <martin(at)bugs(dot)unl(dot)edu(dot)ar> confessed:
>> Is there a function that would give me the date for a given day of year?

> Something like:

> select date_trunc( 'year', now() ) + (extract( doy from now() ) - 1) *
> '1day'::interval;

timestamp + interval arithmetic is likely to give you problems at
daylight savings boundaries, since '1day' will be taken as '24hours'.
A more reliable way to get (what I assume is) the desired result is
to use the date + integer operator:

select date_trunc('year', now())::date + (extract(doy from now()) - 1)::integer;

			regards, tom lane

In response to

Responses

pgsql-sql by date

Next:From: Tom LaneDate: 2004-03-19 15:12:11
Subject: Re: psql: FATAL 1: IDENT authentication failed for user error - Urgent pls
Previous:From: Devrim GUNDUZDate: 2004-03-19 14:23:10
Subject: Re: psql: FATAL 1: IDENT authentication failed for user error

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group