从时间戳中提取月份名称和年份到同一列
Extract month name and year from timestamp to same column
我的时间戳格式为:2020 年 7 月 13 日,6:07 下午
我只想保留月份和年份作为记录。理想格式:2020 年 7 月
我尝试了以下为每个创建一列的操作:
select
count(distinct my_id),
count(distinct my_loc),
to_char(created_at, 'month'),
date_part('year', created_at)
from acc_facts
group by created_at
输出是这样的(4 列):
86 2 may 2,020
能否得到(3列):
86 2 May,2020
你的例子:
select
TO_CHAR(created_at, 'Month') AS "Month",
TO_CHAR(created_at, 'YYYY') AS "Year",
trim(TO_CHAR(created_at, 'Month')) || ', ' || trim(TO_CHAR(created_at, 'yyyy')) as mydate
from acc_facts
结果数据:
January 2022 January, 2022
January 2022 January, 2022
May 2022 May, 2022
March 2022 March, 2022
我的时间戳格式为:2020 年 7 月 13 日,6:07 下午
我只想保留月份和年份作为记录。理想格式:2020 年 7 月
我尝试了以下为每个创建一列的操作:
select
count(distinct my_id),
count(distinct my_loc),
to_char(created_at, 'month'),
date_part('year', created_at)
from acc_facts
group by created_at
输出是这样的(4 列):
86 2 may 2,020
能否得到(3列):
86 2 May,2020
你的例子:
select
TO_CHAR(created_at, 'Month') AS "Month",
TO_CHAR(created_at, 'YYYY') AS "Year",
trim(TO_CHAR(created_at, 'Month')) || ', ' || trim(TO_CHAR(created_at, 'yyyy')) as mydate
from acc_facts
结果数据:
January 2022 January, 2022
January 2022 January, 2022
May 2022 May, 2022
March 2022 March, 2022