Unix Technical Forum

How can I get the first and last date of a week, based on the weeknumber and the year

This is a discussion on How can I get the first and last date of a week, based on the weeknumber and the year within the Pgsql General forums, part of the PostgreSQL category; --> Hello ! I have a week number (ISO 8601) and a year, based on theses values, I would like ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 12:20 AM
Bruno Baguette
 
Posts: n/a
Default How can I get the first and last date of a week, based on the weeknumber and the year

Hello !

I have a week number (ISO 8601) and a year, based on theses values, I
would like to get the first and the last dates of that week.

How I can do that ?

The only solution is doing a big generate_series to build a subset that
contains the week of all the dates between the 01/01 || year and the
31/12 || year. But I find that solution quite dirty and ressources
consumming.

Is there a cleanest way to do that ?

Many thanks in advance !

Regards,

--
Bruno Baguette - bruno.baguette@gmail.com



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 12:20 AM
Adrian Klaver
 
Posts: n/a
Default Re: How can I get the first and last date of a week, based on the week number and the year

On Tuesday 26 February 2008 5:32 pm, Bruno Baguette wrote:
> Hello !
>
> I have a week number (ISO 8601) and a year, based on theses values, I
> would like to get the first and the last dates of that week.
>
> How I can do that ?
>
> The only solution is doing a big generate_series to build a subset that
> contains the week of all the dates between the 01/01 || year and the
> 31/12 || year. But I find that solution quite dirty and ressources
> consumming.
>
> Is there a cleanest way to do that ?
>
> Many thanks in advance !
>
> Regards,


This will get you to the Monday of the week .

select to_date('9 08','IW YY');
to_date
------------
2008-02-25
(1 row)

Where 9 is the ISO week number and 08 is the year. See below for more details:
http://www.postgresql.org/docs/8.2/i...ormatting.html
--
Adrian Klaver
aklaver@comcast.net

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 12:20 AM
brian
 
Posts: n/a
Default Re: How can I get the first and last date of a week, basedon the week number and the year

Bruno Baguette wrote:
> Hello !
>
> I have a week number (ISO 8601) and a year, based on theses values, I
> would like to get the first and the last dates of that week.
>
> How I can do that ?
>
> The only solution is doing a big generate_series to build a subset that
> contains the week of all the dates between the 01/01 || year and the
> 31/12 || year. But I find that solution quite dirty and ressources
> consumming.
>
> Is there a cleanest way to do that ?
>


Use pl/Perl and the Date::Calc module.

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 12:20 AM
Tom Lane
 
Posts: n/a
Default Re: How can I get the first and last date of a week, based on the week number and the year

Adrian Klaver <aklaver@comcast.net> writes:
> On Tuesday 26 February 2008 5:32 pm, Bruno Baguette wrote:
>> I have a week number (ISO 8601) and a year, based on theses values, I
>> would like to get the first and the last dates of that week.


> This will get you to the Monday of the week .


> select to_date('9 08','IW YY');
> to_date
> ------------
> 2008-02-25
> (1 row)


date_trunc('week', ...) would probably be a more convenient way of doing
that.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 12:20 AM
Adrian Klaver
 
Posts: n/a
Default Re: How can I get the first and last date of a week, based on the week number and the year

On Tuesday 26 February 2008 8:39 pm, Tom Lane wrote:
> Adrian Klaver <aklaver@comcast.net> writes:
> > On Tuesday 26 February 2008 5:32 pm, Bruno Baguette wrote:
> >> I have a week number (ISO 8601) and a year, based on theses values, I
> >> would like to get the first and the last dates of that week.

> >
> > This will get you to the Monday of the week .
> >
> > select to_date('9 08','IW YY');
> > to_date
> > ------------
> > 2008-02-25
> > (1 row)

>
> date_trunc('week', ...) would probably be a more convenient way of doing
> that.
>
> regards, tom lane


I am not sure I follow. The OP has a ISO week number and a year and wants
dates. I thought date_trunc('field',source) requires a timestamp or interval
as its source.
--
Adrian Klaver
aklaver@comcast.net

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 12:20 AM
Tom Lane
 
Posts: n/a
Default Re: How can I get the first and last date of a week, based on the week number and the year

Adrian Klaver <aklaver@comcast.net> writes:
> On Tuesday 26 February 2008 8:39 pm, Tom Lane wrote:
>> date_trunc('week', ...) would probably be a more convenient way of doing
>> that.


> I am not sure I follow. The OP has a ISO week number and a year and wants
> dates. I thought date_trunc('field',source) requires a timestamp or interval
> as its source.


Oh, you are right ... I misread the question :-(

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 12:20 AM
Richard Huxton
 
Posts: n/a
Default Re: How can I get the first and last date of a week, basedon the week number and the year

Bruno Baguette wrote:
> Hello !
>
> I have a week number (ISO 8601) and a year, based on theses values, I
> would like to get the first and the last dates of that week.


Broken down step-by-step. End of week left as an exercise

SELECT
doy,
EXTRACT(dow FROM doy) AS offset,
(doy - EXTRACT(dow FROM doy) * '1 day'::interval)::date AS start_of_week
FROM
(SELECT ('2008-01-04'::date + 8 * '1 week'::interval)::date AS doy)
AS foo;

doy | offset | start_of_week
------------+--------+---------------
2008-02-29 | 5 | 2008-02-24
(1 row)

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-10-2008, 12:20 AM
Alban Hertroys
 
Posts: n/a
Default Re: How can I get the first and last date of a week, based on the week number and the year

On Feb 27, 2008, at 2:32 AM, Bruno Baguette wrote:

> Hello !
>
> I have a week number (ISO 8601) and a year, based on theses values,
> I would like to get the first and the last dates of that week.
>
> How I can do that ?
>
> The only solution is doing a big generate_series to build a subset
> that contains the week of all the dates between the 01/01 || year
> and the 31/12 || year. But I find that solution quite dirty and
> ressources consumming.
>
> Is there a cleanest way to do that ?


You can use to_date for most of that, like:
development=> select to_date('01 02 2008', 'ID IW YYYY') AS start,
to_date('07 02 2008', 'ID IW YYYY') AS end;
start | end
------------+------------
2008-01-07 | 2008-01-07
(1 row)

I'm a bit surprised that specifying the weekdays doesn't make any
difference here, maybe it's my version?:

development=> select version();
version
------------------------------------------------------------------------
------------------------
PostgreSQL 8.2.4 on i386-portbld-freebsd6.2, compiled by GCC cc
(GCC) 3.4.6 [FreeBSD] 20060305
(1 row)


Anyway, you can solve that by adding an interval '6 days' to the end
result.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.


!DSPAM:737,47c525af233091991417831!



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:53 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com