Louis Lee
Louis Lee

Reputation: 281

Get the start and end date by passing week number and year

I am using EXTRACT(WEEK from doc_date) to extract the week number from a given datetime.

Is it possible to extract the start and end date by passing week number and year from Postgres?

Upvotes: 0

Views: 1339

Answers (1)

McNets
McNets

Reputation: 10807

You can add an INTERVAL of X weeks to the first date of the year, and 6 more days to get the last date.

select '2017-01-01'::date + interval '3 week' as StartWeekDate,
        ('2017-01-01'::date + interval '3 week') + interval '6 day' as EndWeekDate;

A reduced version:

select '2017-01-01'::date + interval '3 week' as StartWeekDate,
        ('2017-01-01'::date + interval '3 week 6 day') as EndWeekDate;

Upvotes: 3

Related Questions