Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current week number for current month

Tags:

sql

postgresql

I used following query to get current week

select extract(week from current_timestamp) and it is showing 34 thats fine.

But how do i get the current week number of the current month.

like image 234
Prabhakaran Avatar asked Aug 23 '13 09:08

Prabhakaran


3 Answers

You can find the first day of this week with:

trunc('week', current_date)

And the first day of the first week of this month with:

trunc('week', date_trunc('month', current_date))

Subtract the two to find the in-month weeknumber:

extract('day' from date_trunc('week', current_date) -
                   date_trunc('week', date_trunc('month', current_date))) / 7 + 1
like image 74
Andomar Avatar answered Oct 11 '22 05:10

Andomar


Try the following

SELECT to_char('2016-05-01'::timestamp, 'W');
like image 27
Aley Avatar answered Oct 11 '22 06:10

Aley


You can find week number by using day as well like:

select ((date_part('day', current_date)::integer - 1) / 7) +1;
like image 28
Shashank Avatar answered Oct 11 '22 06:10

Shashank