在hive中作为日期铸造,结果不正确

kqhtkvqz  于 8个月前  发布在  Hive
关注(0)|答案(1)|浏览(84)

您好,我有以下查询:

select 
first_tran,
cast(substr(first_tran,1,6) as int) as yyyymm,
from_unixtime(unix_timestamp(substr(first_tran,1,6) , 'yyyymm'))

from
t.table

其中first_tran将format作为字符串:例如20190804。
但我得到以下信息:

我想计算两个YYYYMM值之间的差异,例如:第一个_tran列,比如202308。然而,我不确定如何做到这一点时,上述是没有被投的权利(最后一列)。为什么最后一列显示的是2019-01-01而不是201908?有没有更简单的方法找到两个日期之间的月份?(我想在几个月内)

ghhkc1vu

ghhkc1vu1#

1.月份的日期不正确。
请注意,月份在Hive是MM大写。小写的mm是分钟。如果你看看你的输出,你可以看到它显示8分钟,这是正确的。

select 
first_tran,
cast(substr(first_tran,1,6) as int) as yyyymm,
from_unixtime(unix_timestamp(substr(first_tran,1,6) , 'yyyyMM'))

1.在hive(版本高于1.3)中,你可以使用months between()来计算两个给定日期/时间戳之间的月份。

select  months_between('2023-01-10', '2020-01-10'); --result will be a fraction

相关问题