问题
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