pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Creager <Robert_Creager@LogicalChaos.org>
Cc: Martin Marques <martin@bugs.unl.edu.ar>
Cc: pgsql-sql@postgresql.org
Subject: Re: inverse of "day of year"
Date: Fri, 19 Mar 2004 10:10:05 -0500
Message-ID: <24189.1079709005@sss.pgh.pa.us> (raw)
In-Reply-To: <20040319064714.405b304e.Robert_Creager@LogicalChaos.org>
References: <200403190906.17641.martin@bugs.unl.edu.ar>
	<20040319064714.405b304e.Robert_Creager@LogicalChaos.org>

Robert Creager <Robert_Creager@LogicalChaos.org> writes:
> Martin Marques <martin@bugs.unl.edu.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



view thread (9+ messages)  latest in thread

Message-ID: <24189.1079709005@sss.pgh.pa.us>
Permalink:  ../24189.1079709005@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24189.1079709005@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, Robert_Creager@LogicalChaos.org, martin@bugs.unl.edu.ar
  Subject: Re: inverse of "day of year"
  In-Reply-To: <24189.1079709005@sss.pgh.pa.us>

* 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