我有一个配置单元表的情况,要将数字字符串的两个字段(T1和T2)转换为日期时间戳格式“ YYYY-MM-DD hh:mm:ss.SSS”,并找出两者的区别。我尝试了两种方法:
Select CAST(regexp_replace(substring(t1, 1,17),'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})','$1-$2-$3 $4:$5:$6.$7') as timestamp), CAST(regexp_replace(substring(t2, 1,17),'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})','$1-$2-$3 $4:$5:$6.$7') as timestamp), CAST(regexp_replace(substring(t1, 1,17),'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})','$1-$2-$3 $4:$5:$6.$7') as timestamp) - CAST(regexp_replace(substring(t2, 1,17),'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})','$1-$2-$3 $4:$5:$6.$7') as timestamp) as time_diff
from tab1
并且将输出作为
Select from_unixtime (unix_timestamp(substring(t1,1,17),'yyyyMMddhhmmssSSS'),'yyyy-MM-dd hh:mm:ss.SSS'), from_unixtime (unix_timestamp(substring(t2,1,17),'yyyyMMddhhmmssSSS'),'yyyy-MM-dd hh:mm:ss.SSS'), from_unixtime (unix_timestamp(substring(t1,1,17),'yyyyMMddhhmmssSSS'),'yyyy-MM-dd hh:mm:ss.SSS') - from_unixtime (unix_timestamp(substring(t2,1,17),'yyyyMMddhhmmssSSS'),'yyyy-MM-dd hh:mm:ss.SSS') as time_diff
from tab1;
并获得输出为
我不清楚为什么输出会有所不同。
方法2最终等于这样的东西
select ('2020-04-12 01:30:02.000' - '2020-04-12 01:29:43.000') as time_diff;
您不能像这样减去日期。必须使用DateDiff。
仅当存在差异时,Hive中的DateDiff返回> 0,否则您将得到零。
unix_timestamp()给您纪元时间,即。自UNIX时代以来的时间(以秒为单位)1970-01-01 00:00:00时间戳将提供日期和时间,即YYYY-MM-DD T HH:MI:SS因此,一种准确的方法是将字符串时间戳转换为unix_timestamp(),减去后再使用from_unixtime()返回例如。
select from_unixtime(unix_timestamp('2020-04-12 01:30:02.000') - unix_timestamp('2020-04-12 01:29:43.000'))