select extract(dow from datefield)
从0到6提取一个数字,其中0是星期日;有没有办法在SQL中获取星期几,假设星期一开始周(因此0将是星期一)?
答案 0 :(得分:14)
isodow
The day of the week as Monday (1) to Sunday (7)
所以,你只需要从结果中减去1:
psql (9.6.1)
Type "help" for help.
postgres=> select extract(isodow from date '2016-12-12') - 1;
?column?
-----------
0
(1 row)
postgres=>
答案 1 :(得分:8)
使用date_part功能 dow()
这里0 =星期天,1 =星期一,2 =星期二,... 6 =星期六
select extract(dow from date '2016-12-18'); /* sunday */
输出:0
select extract(isodow from date '2016-12-12'); /* Monday */
输出:1
答案 2 :(得分:4)
如果要使用工作日的文本版本,则可以使用to_char(date, format)
函数提供日期和所需的格式。
根据https://www.postgresql.org/docs/current/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE,我们具有以下可用于日期的格式选项。我已经显示了一些输出示例。根据文档,英文缩写的日值是3个字符长,其他语言环境可能会有所不同。
select To_Char("Date", 'DAY'), * from "MyTable"; -- TUESDAY
select To_Char("Date", 'Day'), * from "MyTable"; -- Tuesday
select To_Char("Date", 'day'), * from "MyTable"; -- tuesday
select To_Char("Date", 'dy'), * from "MyTable"; -- tue
select To_Char("Date", 'Dy'), * from "MyTable"; -- Tue
select To_Char("Date", 'DY'), * from "MyTable"; -- TUE
答案 3 :(得分:1)
with a as (select extract(isodow from date '2020-02-28') - 1 a ),
b as(select CASE
WHEN a.a =1 THEN 'Monday'
WHEN a.a =2 THEN 'Tuesday'
WHEN a.a=3 THEN 'Wednesday'
WHEN a.a=4 THEN 'Thursday'
WHEN a.a=5 THEN 'Friday'
WHEN a.a=6 THEN 'Saturday'
WHEN a.a=7 THEN 'Sunday'
ELSE 'other'
END from a )
select * from b;