我有一个具有YYYY-MM-DD的Birthdate字段。
我正在使用以下声明:
select
farm_fingerprint(cast(P.Key as string)) as person_id
, EXTRACT(YEAR from P.BirthDate) as year_of_birth
, EXTRACT(MONTH from P.BirthDate) as month_of_birth
, EXTRACT(DAY from P.BirthDate) as day_of_birth
, DATETIME(TIMESTAMP (CONCAT(CAST(P.BirthDate as string), ' 00:00:00')))
, P.Birthdate
from person P;
我的问题是关于BirthDate字段。如何确保这是转换前的有效日期?
如果值不好,EXTRACT()将失败。 COALESCE()是否可以处理失败的函数?如果没有,我该如何补偿并设置null?
由于
答案 0 :(得分:1)
我们有类似的要求,即使用SAFE_CAST()过滤掉无效记录
select * from invalid_date_fix_test
WHERE
SAFE_CAST(dt_date AS TIMESTAMP) is not null and
timestamp(dt_date) > TIMESTAMP_SUB(TIMESTAMP_TRUNC('2019-03-24 00:37:35', DAY),INTERVAL 3 DAY)
答案 1 :(得分:0)
您好,我遇到了数据格式问题,因为数据来自多个国家/地区,其本地格式为mm/dd/yyyy
和dd/mm/yyyy
,我将在下面进行处理。希望它可以帮助或给出方向。
case when departureDate is null or departureDate = '' then null
when cast(substr(departureDate,1,2) as int64) > 12 then
cast(parse_date('%d/%m/%Y',departureDate) as datetime)
else cast(parse_date('%m/%d/%Y',departureDate) as datetime) end as departureDate