首页 > 解决方案 > Hive SQL-向现有时间戳添加(不附加)秒

问题描述

我正在尝试将秒数添加到现有时间戳以查找结束时间。我在一个字段中有开始时间,在一个字段中有总持续时间。我怎么能加。我尝试使用 date_add 函数,但这不起作用。

Select
      testdate, 
      DATE_ADD ( testdate,CAST (testduration as INT) ) as Test_end_date  
from <DBName>  limit 10;
TestDate is this format= 9/14/2017 16:33:25.000000
Testduration is in seconds e.g: 144 seconds or so

它添加到日期而不是秒组件。

任何帮助表示赞赏。

试过 Date_Add 功能,不会工作。

标签: sqlhivetimestamp

解决方案


使用 unix_timestamp 转换为秒,添加秒

select from_unixtime(UNIX_TIMESTAMP('9/14/2017 16:33:25.000000','M/dd/yyyy HH:mm:ss.SSSSS')+144, 'yyyy-MM-dd HH:mm:ss.SSSSS')

这将返回标准 Hive 时间戳格式的时间戳,如果需要保留原始格式,请更改输出格式:'M/dd/yyyy HH:mm:ss.SSSSS' 作为from_unixtime()函数的第二个参数。


推荐阅读