跳过格式错误的日期分析presto



我正在使用以下查询来解析presto中的日期:

SELECT date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p') from table t

样品日期为:4/11/2021 12:30:00 PM

但有时我们得到的不是日期,不能像"testdate"(任何不是日期的字符串(那样解析

如何在查询中跳过此类日期?我的查询应该是:

select date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p') 
from table t 
where <skip the date that does not parse>

使用try()。通常date_parse()在错误的日期格式上失败。如果您添加try(),它将为错误的日期生成NULL,您可以像这样过滤NULL记录:

select try(date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p'))
from table t 
--filter rows wich can not be parsed if necessary
where try(date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p')) is not NULL

此外,您还可以尝试使用coalize((解析不同的格式,以选择成功解析的:

select
coalesce( try(date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p')), --try format1
try(date_parse(t.up_date, '%Y/%m/%d'))  --try format2
)
from table t 
where --filter only parsed dates
coalesce( try(date_parse(t.up_date, '%c/%e/%Y %l:%i:%s %p')), --try format1
try(date_parse(t.up_date, '%Y/%m/%d'))  --try format2
) is not NULL;

通过这种方式,您可以尝试解析数据中的不同格式。

最新更新