一、定义
TIMESTAMP为HIVE的一种时间类型,通常用来存储时间戳,存储了从1970-01-01 00:00:00GMT经过了多长时间长度,其格式通常为:yyyy-MM-dd HH:mm:ss.SSS
其中:yyyy 表示年份/MM 表示月份/dd 表示日期/HH 表示小时/mm 表示分钟/ss 表示秒/SSS 表示毫秒
二、常见用法
用法 | 示例 |
时间戳转TIMESTAMP类型 | timestamp() |
2.1 时间戳转timestamp类型
select timestamp(1692269092799)
结果:
2023-08-17 18:44:52.799 |
2.2 timestamp类型比较大小,可直接转bigint做加减法
select cast(end_time as bigint) - cast(start_time as bigint) as diff1
,cast(diff/1000 as bigint) as diff2
from
(
select timestamp(1692269092799) as start_time
,timestamp(1692969092799) as end_time
,1692969092799-1692269092799 as diff
)a
结果:
diff1 | diff2 |
700000 | 700000 |
2.3 timestamp类型转日期
select to_date(start_time)
from
(
select timestamp(1692269092799) as start_time
)a
结果:
2023-08-17 |