在hive中计算两个日期的天数差

7d7tgy0s  于 2021-04-08  发布在  Hive
关注(0)|答案(1)|浏览(1059)

我试图显示 "empdate "列和当前日期之间的天数差小于等于365的记录。

列empdate是varchar数据类型.我已经写了下面的查询,但不能实现结果。其中我得到的所有记录是大于365的当前日期和empdate之间。

select * from table where 
cast(datediff(from_unixtime(unix_timestamp(current_date 'yyyy-MM-dd'),'yy-MM-dd'),
from_unixtime(unix_timestamp(cast(empdate as string)'yyMMdd'),'yy-MM-dd') as int)<=365;
syqv5f0l

syqv5f0l1#

下面的查询可能对你有所帮助。

select datediff(to_date(from_unixtime(unix_timestamp(current_date),'yyyy-MM-dd')),
to_date(from_unixtime(unix_timestamp(empdate, 'yyMMdd')))) <=365 as daydiff 
from time_table;

测试来自hive的查询。

create table if not exists time_table(empdate string, empvalue string) row format
 delimited fields terminated by ',' stored as textfile;

insert into table time_table values ('101001','A'),('200101','B'),
('100619','C'),('110707','D');

hive> select *, datediff(to_date(from_unixtime(unix_timestamp(current_date),'yyyy-MM-dd')),
to_date(from_unixtime(unix_timestamp(empdate, 'yyMMdd')))) <=365 as daydiff 
from time_table;

OK
101001  A       false
200101  B       true
100619  C       false
110707  D       false

相关问题