pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sergey Konoplev <gray.ru@gmail.com>
To: Ireneusz Pluta <ipluta@wp.pl>
Cc: pgsql-sql@postgresql.org
Subject: Re: Week to date function
Date: Fri, 26 Mar 2010 14:02:02 +0300
Message-ID: <c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com> (raw)
In-Reply-To: <4BAC9107.5070701@wp.pl>
References: <4BAB2C15.8090406@wp.pl>
	<c3a7de1f1003250244p440b8244t57319b742fa680f0@mail.gmail.com>
	<4BAC9107.5070701@wp.pl>

> CREATE OR REPLACE FUNCTION your_week2date(double precision, double
> precision) RETURNS SETOF date
> AS
> $_$
> SELECT  day
> FROM (
>   SELECT  (
>               date_trunc('week', ($1::text||'-01-01')::date)
>               + ($2::text||' week')::interval
>               + ( d::text||'  day')::interval
>           )::date  AS  day
>   FROM    generate_series(0, 6) AS d
> )   alias
> WHERE   to_char(day, 'IYYY')::integer = $1
> ORDER
> BY      1
> $_$
> LANGUAGE SQL
> IMMUTABLE

I think it is better to use date_part('year', day) instead of
to_char(...). And may be it is worth to do raise exception when
incorrect week specified.

-- 
Sergey Konoplev

Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp / ICQ: 29353802



view thread (10+ messages)  latest in thread

Message-ID: <c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com>
Permalink:  ../c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com/
Also on:    postgresql.org/message-id/c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com

 · 

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: gray.ru@gmail.com, ipluta@wp.pl
  Subject: Re: Week to date function
  In-Reply-To: <c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com>

* 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