Re: Week to date function

From: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>
To: Ireneusz Pluta <ipluta(at)wp(dot)pl>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Week to date function
Date: 2010-03-26 11:02:02
Message-ID: c3a7de1f1003260402y42a61ca4j657983343760d4d4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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(dot)ru(at)gmail(dot)com / Skype: gray-hemp / ICQ: 29353802

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ireneusz Pluta 2010-03-26 11:47:13 Re: Week to date function
Previous Message Ireneusz Pluta 2010-03-26 10:48:39 Re: Week to date function