如何将我的时间戳更改为雪花格式为 yyyy-01-01 的日期 (sql)
How to change my timestamp into a date formatted as yyyy-01-01 in snowflake (sql)
我需要将一列时间戳 (date_col) 的格式更改为 yyyy-01-01。 date_col
中的一月一日
示例:
actual desired
2021-04-02 00:00:00.000 2021-01-01
1966-05-04 00:00:00.000 1966-01-01
请使用date_trunc
select date_trunc(year,'2021-04-02 00:00:00.000'::date);
+--------------------------------------------------+
| DATE_TRUNC(YEAR,'2021-04-02 00:00:00.000'::DATE) |
|--------------------------------------------------|
| 2021-01-01 |
+--------------------------------------------------+
一种可能是,我认为@Pankaj 展示的更好
select column1::timestamp as col1, year(col1) as "YEAR"
,year || '-' || '01'|| '-' ||'01' as dt
,to_date(dt,'YYYY-MM-DD')
from values
('2021-04-02 00:00:00.000') ,
('1966-05-04 00:00:00.000')
像这样的更新...
update my_table
set date_field = date_from_parts(extract(year from date_field), 1, 1)
我需要将一列时间戳 (date_col) 的格式更改为 yyyy-01-01。 date_col
中的一月一日示例:
actual desired
2021-04-02 00:00:00.000 2021-01-01
1966-05-04 00:00:00.000 1966-01-01
请使用date_trunc
select date_trunc(year,'2021-04-02 00:00:00.000'::date);
+--------------------------------------------------+
| DATE_TRUNC(YEAR,'2021-04-02 00:00:00.000'::DATE) |
|--------------------------------------------------|
| 2021-01-01 |
+--------------------------------------------------+
一种可能是,我认为@Pankaj 展示的更好
select column1::timestamp as col1, year(col1) as "YEAR"
,year || '-' || '01'|| '-' ||'01' as dt
,to_date(dt,'YYYY-MM-DD')
from values
('2021-04-02 00:00:00.000') ,
('1966-05-04 00:00:00.000')
像这样的更新...
update my_table
set date_field = date_from_parts(extract(year from date_field), 1, 1)