extract week number from date postgres

那年仲夏 提交于 2019-12-20 11:52:39

问题


I would like to extract the week number as:

2015-52

from a date formatted as:

2015-12-27

How can I perform this in postgres?

my weeks are calculated from monday to sunday.


回答1:


To get the year and the week in a single character value, use to_char()

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

IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year).

If you need the year and the week number as numbers, use extract

select extract('isoyear' from current_date) as year, 
       extract('week' from current_date) as week;



回答2:


You can do as follows

SELECT EXTRACT(year FROM current_date) FROM table ;


来源:https://stackoverflow.com/questions/34050103/extract-week-number-from-date-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!