postgresql - 在 postgresql 中提取周数的匹配年份

标签 postgresql

在 postgresql 中,extract(week from '2014-12-30'::timestamp) 给出了 2015 年的第 1 周。如何提取与周数对应的相关年份?使用 extract(year ... 给出 2014

最佳答案

因为 week 是 ISO 定义的周,所以您需要 isoyear:

#= select extract(isoyear from '2014-12-30'::timestamp);
 date_part
-----------
      2015
(1 row)

来自docs :

By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year.

In the ISO week-numbering system, it is possible for early-January dates to be part of the 52nd or 53rd week of the previous year, and for late-December dates to be part of the first week of the next year. For example, 2005-01-01 is part of the 53rd week of year 2004, and 2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is part of the first week of 2013. It's recommended to use the isoyear field together with week to get consistent results.

关于postgresql - 在 postgresql 中提取周数的匹配年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249468/

相关文章:

php - CakePHP 无法连接到 Postgres。但是 Pgadmin4 可以

sql - JOIN 子句的括号

postgresql - LOGSTASH:如何设置Logstash以获取所有查询的数据而不仅仅是其中的一部分?

.net - 如何向客户端报告长时间运行的 PostgreSQL 函数的进度

sql - 在单引号中使用 'like'时的Postgres语法错误

postgresql - Heroku 页面 :pull is giving sh: createdb: command not found

postgresql - 为什么 Play 失败并显示 "Driver not found: [org.postgresql.Driver]"?

java - 如何通过 java 将 JSON 插入到 Postgres 中?

php - 为什么 Laravel 调用 execute() 会返回重复的结果?

node.js - 如何使用 nodeJS + multer + sequelize 存储 pdf 文件