当月的当前周数

ove*_*low 8 sql postgresql

我使用以下查询来获取当前周

select extract(week from current_timestamp)它表明34很好.

但是我如何得到当月的当前周数.

And*_*mar 17

你可以找到本周的第一天:

trunc('week', current_date)
Run Code Online (Sandbox Code Playgroud)

本月第一周的第一天:

trunc('week', date_trunc('month', current_date))
Run Code Online (Sandbox Code Playgroud)

减去两个以找到月中的周数:

extract('day' from date_trunc('week', current_date) -
                   date_trunc('week', date_trunc('month', current_date))) / 7 + 1
Run Code Online (Sandbox Code Playgroud)

  • 请记住,这是基于使用ISO-8601的年周数的周数.不是一周#基于月份本身..所以例如:今天2015年2月18日使用上述方法将返回4,而我认为海报正在寻找的是3. (2认同)

Ale*_*ley 5

尝试以下

SELECT to_char('2016-05-01'::timestamp, 'W');
Run Code Online (Sandbox Code Playgroud)