我有一列“ customer_date”,其数据类型为文本。
基于我需要派出新列的时间
条件:
customer_date
01Mar2018 10:03:54
02Mar2018 13:03:54
预期产量
customer_date session
01Mar2018 10:03:54 AM
02Mar2018 13:03:54 PM
我写
时抛出错误select
case EXTRACT(HOUR FROM customer_date)<12 then 'AM' else 'PM' end as session
from my table;
答案 0 :(得分:2)
这在我测试时有效:
select (case when EXTRACT(HOUR FROM customer_date::timestamp) < 12 then 'AM' else 'PM' end) as session
from mytable;
我添加了显式转换和when
。
答案 1 :(得分:1)
您忘了像{Gordon Linoff所说的那样,在WHEN
中添加CASE WHEN
。
有一种简单的方法,使用TO_CHAR代替CASE WHEN
。
习惯TO_CHAR
AM, am, PM or pm
将获得子午线指示器(无期限)
CREATE TABLE myTable(customer_Date timestamp);
insert into myTable values ('01Mar2018 10:03:54'::timestamp);
insert into myTable values ('02Mar2018 13:03:54'::timestamp);
查询1 :
select customer_Date,TO_CHAR(customer_Date::timestamp, 'AM') as session
from mytable
Results :
| customer_date | session |
|----------------------|---------|
| 2018-03-01T10:03:54Z | AM |
| 2018-03-02T13:03:54Z | PM |