YOBOX YOBOX - 1 month ago 10
SQL Question

How to get week start date from week number in postgresql?

I have all week numbers from 20161 to 201640. I want to know what is the start date and end date of week 31.

How can I write query in postgresql to get that?

Answer

To get the start date, simply convert the week number to a date using to_date()

If you are using ISO week numbers use:

select to_date('201643', 'iyyyiw');

Otherwise use:

select to_date('201643', 'yyyyww');

To get the end date, just add 7 to resulting date: to_date('201643', 'iyyyiw') + 7