Re: get first / last date of given week - Mailing list pgsql-general

From Lee Harr
Subject Re: get first / last date of given week
Date
Msg-id BAY2-F4gjvM9KfYDovv0009dd56@hotmail.com
Whole thread Raw
In response to get first / last date of given week  ("Lee Harr" <missive@hotmail.com>)
List pgsql-general
>Have a look at this simpler non looping version of week_start()
>

That is a nice idea.  I had to modify it a bit in order to get
the same answers as my other function ...

CREATE or REPLACE FUNCTION week_start2(integer, integer) RETURNS date AS '
    DECLARE
        pyear ALIAS FOR $1;
        pweek ALIAS FOR $2;

        year_start date;
        year_start_dow integer;
        week_interval interval;
        week_date date;
        week_year integer;
        dow_interval interval;

    BEGIN
        IF pweek < 1 THEN
            RAISE EXCEPTION ''No week numbers less than 1'';
        END IF;
        IF pweek > 53 THEN
            RAISE EXCEPTION ''No week numbers over 53'';
        END IF;

        year_start := to_date(pyear, ''yyyy'');
        year_start_dow := date_part(''dow'', year_start);
        week_interval := pweek-1 || '' week'';
        dow_interval := year_start_dow || '' day'';
        week_date := year_start - year_start_dow + 1 + week_interval;
        week_year := extract(year FROM week_date);

        IF week_year > pyear THEN
            RAISE EXCEPTION ''No week 53 in this year'';
        END IF;

    RETURN week_date;
    END;
' LANGUAGE 'plpgsql';

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963


pgsql-general by date:

Previous
From: "Markus Wollny"
Date:
Subject: Re: [OpenFTS-general] AW: tsearch2, ispell, utf-8 and german special characters
Next
From: Oliver Elphick
Date:
Subject: Re: sorting and spaces in postgresql with en_US locale