Re: Week to date function

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

Sergey Konoplev pisze:
>> 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(...).
this might cut first or last week in the year, like this:

SELECT your_week2date(2009, 52) ;
your_week2date
----------------
2009-12-28
2009-12-29
2009-12-30
2009-12-31
2010-01-01 cut
2010-01-02 cut
2010-01-03 cut
(7 rows)

which is not what I want when playing with isoweeks.
> And may be it is worth to do raise exception when
> incorrect week specified.
>
but, maybe, controlled by an additonal parameter saying if one prefers
to have exception or rather an empty resultset

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jorge Godoy 2010-03-27 18:08:53 Re: Week to date function
Previous Message Sergey Konoplev 2010-03-26 11:02:02 Re: Week to date function