从postgres日期提取周数

问题描述 投票:10回答:1

我想提取周数:

2015-52

从格式为的日期:

2015-12-27

我怎样才能在postgres中执行此操作?

我的周数是从星期一到星期日计算的。

time postgresql-9.1
1个回答
21
投票

要使用单个字符值获取年份和周数,请使用to_char()

select to_char(current_date, 'IYYY-IW');

IW返回年份和周数,因为defined in the ISO standardIYYY返回相应的年份(可能是前一年)。

如果您需要年份和周数作为数字,请使用extract

select extract('isoyear' from current_date) as year, 
       extract('week' from current_date) as week;
© www.soinside.com 2019 - 2024. All rights reserved.