Re: [SQL] Start and End Day of a Week

From: Luis Magaña <joe666(at)gnovus(dot)com>
To: Postgresql General Mail List <pgsql-general(at)postgresql(dot)org>, Postgresql SQL Mail List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: [SQL] Start and End Day of a Week
Date: 2003-02-07 23:52:39
Message-ID: 1044661959.1123.8.camel@kerberus
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Hi,

I've found my own answer... this is in case somebody needs it someday,
it seems that works properly in PG 7.3, one function gives the start of
the week and the other one the end:

CREATE OR REPLACE FUNCTION weekstart(int4,int4)
RETURNS TIMESTAMP
AS'
DECLARE
year ALIAS FOR $1;
week ALIAS FOR $2;
startsin TIMESTAMP;
BEGIN

SELECT INTO startsin
(SELECT CAST(year || ''-01-01'' AS TIMESTAMP) -
(date_part(''dow'',CAST(year || ''-01-01'' AS
TIMESTAMP))||'' days'')::INTERVAL) +
((week*7)||'' days'')::INTERVAL - ''7
days''::INTERVAL;

RETURN startsin;

END;
' LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION weekend(int4,int4)
RETURNS TIMESTAMP
AS'
DECLARE
year ALIAS FOR $1;
week ALIAS FOR $2;
endsin TIMESTAMP;
BEGIN

SELECT INTO endsin
(SELECT CAST(year || ''-01-01'' AS TIMESTAMP) -
(date_part(''dow'',CAST(year || ''-01-01'' AS TIMESTAMP))||''
days'')::INTERVAL) +
((week*7)||'' days'')::INTERVAL - ''1 days''::INTERVAL;

RETURN endsin;

END;
' LANGUAGE 'plpgsql';

On Fri, 2003-02-07 at 14:57, Luis Magaña wrote:
> Hi,
>
> how can I get the first and last day of a week given the year and the
> week number ???
>
> example
>
> week 3 of 2003 starts in Jan 12th and ends on Jan 18th.
>
> how to know the last two values given the year and the number of week ?
>
> thanks.
>
> Best Regards.
--
Luis Magaña.
Gnovus Networks & Software.
www.gnovus.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message James C. Ousley 2003-02-08 00:02:56 PL/Pgsql trigger function problem.
Previous Message Ricardo Ryoiti S. Junior 2003-02-07 23:20:49 Re: Soft Updates/FFS and Postgresql

Browse pgsql-sql by date

  From Date Subject
Next Message Frankie 2003-02-08 05:41:56 plpgsql + dblink() question
Previous Message Eduardo 2003-02-07 20:58:59 DATABASE EXAMPLES?