获得postgreSQL的当前周

我一直在网上搜索current_week的正确postgreSQL语法.我搜索了所附的链接但是却无法从中获得任何结果 Date/Time.我的任务是将星期日作为本周的开始.

我尝试了与current_date相同但失败了:

select current_week

postgreSQL必须有当前的一周语法.

解决方法

knowing that提取物(‘dow’来自

The day of the week as Sunday (0) to Saturday (6)

By definition,ISO weeks start on Mondays

您可以通过减少一天来解决方法:

select date_trunc('week',current_date) - interval '1 day' as current_week
  current_week
------------------------
 2016-12-18 00:00:00+00
(1 row)

这是样本:

t=# with d as (select generate_series('2016-12-11','2016-12-28','1 day'::interval) t)
select date_trunc('week',d.t)::date  - interval '1 day' as current_week,extract('dow' from d.t),d.t from d
;
    current_week     | date_part |           t
---------------------+-----------+------------------------
 2016-12-04 00:00:00 |         0 | 2016-12-11 00:00:00+00
 2016-12-11 00:00:00 |         1 | 2016-12-12 00:00:00+00
 2016-12-11 00:00:00 |         2 | 2016-12-13 00:00:00+00
 2016-12-11 00:00:00 |         3 | 2016-12-14 00:00:00+00
 2016-12-11 00:00:00 |         4 | 2016-12-15 00:00:00+00
 2016-12-11 00:00:00 |         5 | 2016-12-16 00:00:00+00
 2016-12-11 00:00:00 |         6 | 2016-12-17 00:00:00+00
 2016-12-11 00:00:00 |         0 | 2016-12-18 00:00:00+00
 2016-12-18 00:00:00 |         1 | 2016-12-19 00:00:00+00
 2016-12-18 00:00:00 |         2 | 2016-12-20 00:00:00+00
 2016-12-18 00:00:00 |         3 | 2016-12-21 00:00:00+00
 2016-12-18 00:00:00 |         4 | 2016-12-22 00:00:00+00
 2016-12-18 00:00:00 |         5 | 2016-12-23 00:00:00+00
 2016-12-18 00:00:00 |         6 | 2016-12-24 00:00:00+00
 2016-12-18 00:00:00 |         0 | 2016-12-25 00:00:00+00
 2016-12-25 00:00:00 |         1 | 2016-12-26 00:00:00+00
 2016-12-25 00:00:00 |         2 | 2016-12-27 00:00:00+00
 2016-12-25 00:00:00 |         3 | 2016-12-28 00:00:00+00
(18 rows)

Time: 0.483 ms

相关文章

文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,...
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb...
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才...
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation...
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgr...
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解...