根据时间戳派生新列
Derive a new column based on Time stamp
我有一个数据类型为文本的列 'customer_date'。
根据我需要导出新列的时间
条件:
- when customer_date <12 then 'AM' else 'PM
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;
这在我测试时有效:
select (case when EXTRACT(HOUR FROM customer_date::timestamp) < 12 then 'AM' else 'PM' end) as session
from mytable;
我添加了显式转换和 when
。
您忘记在 CASE WHEN
中添加 WHEN
,正如@Gordon Linoff 所说。
有一个简单的方法,使用TO_CHAR代替CASE WHEN
。
用于TO_CHAR
AM, am, PM or pm
will get meridiem indicator (without periods)
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
| customer_date | session |
|----------------------|---------|
| 2018-03-01T10:03:54Z | AM |
| 2018-03-02T13:03:54Z | PM |
我有一个数据类型为文本的列 'customer_date'。
根据我需要导出新列的时间
条件:
- when customer_date <12 then 'AM' else 'PM
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;
这在我测试时有效:
select (case when EXTRACT(HOUR FROM customer_date::timestamp) < 12 then 'AM' else 'PM' end) as session
from mytable;
我添加了显式转换和 when
。
您忘记在 CASE WHEN
中添加 WHEN
,正如@Gordon Linoff 所说。
有一个简单的方法,使用TO_CHAR代替CASE WHEN
。
用于TO_CHAR
AM, am, PM or pm
will get meridiem indicator (without periods)
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
| customer_date | session |
|----------------------|---------|
| 2018-03-01T10:03:54Z | AM |
| 2018-03-02T13:03:54Z | PM |