如何使用 extract(dow from date) 调整查询中的偏移量,使星期一为 0 - Postgres

How can I adjust the offset in my query using extract(dow from date) so that Monday is 0 - Postgres

我使用的是 Postgres,我想按如下方式获取星期几:

(Monday: 0, Tuesday: 1 ,... Sunday:6)

但在 Postgres 函数中使用以下 SQL

extract(dow from current_date)

Postgres 回复了我:

(Sunday: 0, Monday: 1 ,... Saturday:6)

如何使用 extract(dow from date) 调整查询中的偏移量,使星期一为 0 而不是星期日?

而且我想知道为什么以下 Postgres 函数:

date_trunc('week',(current_date)

使用与提取 dow 不同的逻辑。

(Monday: 0 -> Week Start, Tuesday: 1 ,... Sunday:6 -> Week End)

使用isodow代替dow

Quote from the manual

isodow
      The day of the week as Monday (1) to Sunday (7)

然后简单地从结果中减去1:

extract(isodow from current_date) - 1