将日期名称转换为数字-e-g-星期一转换为1

时间:2018-12-27 10:47:02

标签: amazon-redshift

我没有时间戳表。我只需要使用名称字符串进行转换,

SELECT day_of_week;

输出应类似于

day_of_week sum
Friday       5
Monday       1
Saturday     6
Sunday       7
Thursday     4
Tuesday      2
Wednesday    3

1 个答案:

答案 0 :(得分:0)

如果是时间戳,则可以使用isodow。参见doc

isodow

星期几为星期一(1)星期日(7)

SELECT day_of_week, EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40') as sum FROM your_table;

但是您没有该列。因此,您可以像下面一样使用CASE...WHEN-

SELECT day_of_week,
       CASE WHEN day_of_week='Monday' THEN 1
            WHEN day_of_week='Tuesday' THEN 2
            WHEN day_of_week='Wednesday' THEN 3
            WHEN day_of_week='Thursday' THEN 4
            WHEN day_of_week='Friday' THEN 5
            WHEN day_of_week='Saturday' THEN 6
            ELSE 7
       END as sum
    FROM your_table;

查看文档:{​​{3}}