使用配置单元函数验证日期值

xtfmy6hx  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(270)

我有一个字符串列,它的值是日期。有些是有效的( yyyy-MM-dd )有些不是。如何仅使用配置单元筛选有效和无效的配置单元?我不能使用自定义udf或spark,所以它只能使用配置单元函数。

select * from date_test;
+-------------------+--+
| date_test.mydate  |
+-------------------+--+
| 2018-12-13        | => valid
| 2018-13-12        | => invalid
| 2018-04-31        | => invalid
+-------------------+--+

select mydate,to_date(mydate) from date_test;
+-------------+-------------+--+
|   mydate    |     _c1     |
+-------------+-------------+--+
| 2018-12-13  | 2018-12-13  |
| 2018-13-12  | 2019-01-12  | => to_date() casts it to valid value
| 2018-04-31  | 2018-05-01  | => to_date() casts it to valid value
+-------------+-------------+--+
q3qa4bjr

q3qa4bjr1#

我已经设法做到了,但我愿意接受其他更好的方法。

//valid date values
select 
   mydate,
   to_date(mydate) 
from 
   date_test
where 
   mydate = to_date(mydate); 

+-------------+-------------+--+
|   mydate    |     _c1     |
+-------------+-------------+--+
| 2018-12-13  | 2018-12-13  |
+-------------+-------------+--+

//invalid date values
select 
   mydate,
   to_date(mydate) 
from 
   date_test
where 
   mydate <> to_date(mydate); 

+-------------+-------------+--+
|   mydate    |     _c1     |
+-------------+-------------+--+
| 2018-13-12  | 2019-01-12  |
| 2018-04-31  | 2018-05-01  |
+-------------+-------------+--+

相关问题