验证日期在配置单元中是否有效?

verify the date is valid or not in hive?

我有 yyyyMMdd 格式的日期列。我想检查日期是否有效。 在 informatica 中,该函数可用 CASE WHEN IS_DATE(TO_CHAR(DT),'YYYYMMDD') = 0 THEN TO_DATE('99991231','YYYYMMDD') ELSE TO_DATE(TO_CHAR(DT),'YYYYMMDD') END AS EFF_DT

因为在 hive 中替代 'is_date' 功能不可用,如何在 hive 中实现相同的功能。

使用正则表达式:

case when regexp_extract(date_column,'(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])',0) = '' 
     then 'not valid date' 
     else 'valid date' 
end

根据您的日期要求编辑正则表达式。

您可以使用宏:

create temporary macro isDate(s string)
case when regexp_extract(s,'(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])',0) = '' 
         then false 
         else true 
    end;

然后在你的SQL中使用它:

select * from table where isDate(date_col); 

对于 yyyy-MM-dd 格式,您可以使用 cast(date_col as date):

create temporary macro isDate(s string)
case cast(s as date) is not null then true else false end

假设您的日期格式为 yyyy-MM-dd,即 2018-07-20

获取全部无效

Select required_column_name from table_name where cast(date_column_name as date) is NULL; 

获取所有有效

Select required_column_name from table_name where cast(date_column_name as date) is not NULL;