472,331 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

get first / last date of given week

I wrote a function to return the first date of a given week
(and a few related functions) :
-- return the first date in the given week
CREATE or REPLACE FUNCTION week_start(integer, integer) RETURNS date AS '
DECLARE
pyear ALIAS FOR $1;
pweek ALIAS FOR $2;

year_text text;
year_start date;

week_text text;
interval_text text;
week_interval interval;
week_date date;
week_year integer;

day_interval interval := ''1 day'';

wk integer;

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

year_text := pyear-1 || ''-12-15'';
year_start := year_text::date;

interval_text := pweek || '' week'';
week_interval := interval_text::interval;
week_date := year_start + week_interval;

wk := extract(week FROM week_date);
WHILE wk <> pweek LOOP
week_date := week_date + day_interval;
wk := extract(week FROM week_date);
END LOOP;

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';
-- return the first date in this current week
CREATE or REPLACE FUNCTION week_start() RETURNS date AS '
DECLARE
yr integer;
wk integer;

BEGIN
yr := extract(year from current_date);
wk := extract(week from current_date);

RETURN week_start(yr, wk);

END;
' LANGUAGE 'plpgsql';
-- return the last date in the given year/week
CREATE or REPLACE FUNCTION week_end(integer, integer) RETURNS date AS '
DECLARE
pyear ALIAS FOR $1;
pweek ALIAS FOR $2;

BEGIN
RETURN week_start(pyear, pweek) + interval ''6 days'';

END;
' LANGUAGE 'plpgsql';
-- return the last date in the current week
CREATE or REPLACE FUNCTION week_end() RETURNS date AS '
DECLARE
yr integer;
wk integer;

BEGIN
yr := extract(year from current_date);
wk := extract(week from current_date);

RETURN week_end(yr, wk);

END;
' LANGUAGE 'plpgsql';

Have a reinvented a wheel here? (badly? ;o)
Is there a cookbook where I should post this code?

__________________________________________________ _______________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
0 2747

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
If anyone can help me out with a good way to do this in javascript I would greatly appreciate it. I need to compute three dates in javascript -...
11
by: Dennis Marks | last post by:
There seems to be a major program with the automatic display of the last modified date. Using the javascript "document.lastModified" sometimes...
5
by: Hardy Wang | last post by:
Hi: Are there any algorithms I can use, that based on a given date and a culture code (different cultures may have different beginning of week), I...
1
by: Hadi | last post by:
Hello, I have to DateTime structure, how do I calculate if date A is on the same week with date B or last week of date B? Thanks, Hadi
1
by: Martin Emanuelsson | last post by:
Hello, Knowing year and weeknumber of this year, I'm looking for some way to get the date of the first and last day of that particular week. ...
2
by: Benz | last post by:
Hello all, Can anyone please tell me how to find the first and last date of a week when the year and the week number is given? The week model...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. ...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
HI I have a stored procedure that returns data with a date field in the form of a DateTime type. I need to place data in variables based on days of...
4
by: Charlotte | last post by:
Hi, is it possible to determine the first and the last day of a week (with classic ASP) nu = "06/01/2008" week = (DatePart("ww",nu)) ...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.