Varchar 到日期或雪花中的时间戳

Varchar to Date or Timestamp in snowflake

我有一列 date_sent 是 varchar 格式。我只想捕获日期。

select
date_sent,
TRY_TO_TIMESTAMP(date_sent,'MM/DD/YYYY HH:MI:SS AM') send_ts,

send_ts::date as send_date

也尝试了 TO_DATE(date_sent) send_ts 但出现错误:

Date '1/6/2022 6:01:00 PM' is not recognized

输出:

由于时间戳采用 varchar 格式,您可以使用以下内容捕获日期部分

select substr(date_sent,1,position(' ',ts))::date

示例:

with data as (select '1/6/2022 6:01:00 PM' date_sent)
select substr(date_sent,1,position(' ',date_sent))::date dt
from data

returns

2022-01-06

你也可以使用to_date func 和 covert:

with tbl as (select '1/6/2022 6:01:00 PM'::varchar dt)
select TO_date(dt, 'mm/dd/yyyy hh:mi:ss PM')::date dt
from tbl;