将HH:MM:S转换为秒int

时间:2019-07-05 04:53:53

标签: sql amazon-redshift datetime-conversion

我有两列的格式为YYYY-MM-DD HH:MM:SS 当我对这两列进行区别时,结果的格式为HH:MM:SS

我想将此结果转换为秒。我该怎么做?

示例:

2019-06-15 19:27:33.045000 | 2019-06-15 19:27:22.298000 | 00:00:10.747

2 个答案:

答案 0 :(得分:0)

您可以使用DATEDIFF ( datepart, {date|timestamp}, {date|timestamp} )功能获取更多信息check here

例如

 select datediff(second, '0','05:15:40');

答案 1 :(得分:-1)

您可以尝试以下方法:

Declare @time varchar(50)= '05:15:40'
select Sum(Left(@time,2) * 3600 + substring(@time, 4,2) * 60 + substring(@time, 7,2)) 
as seconds

OR

Declare @time varchar(50)= '05:15:40'
SELECT seconds = DATEPART(SECOND, @time) +  60 * DATEPART(MINUTE, @time) + 
3600 * DATEPART(HOUR, @time)