获得两次时间格式的差异

时间:2018-03-26 06:21:02

标签: sql firebird datediff

我试图在firebird sql中获得两个不同时间的时间格式差异。我尝试减去两次,但结果不是时间格式。

  TIME_IN TIME,
  TIME_OUT TIME,
  DIFFERENCE TIME
SELECT 
  TIME_IN,
  TIME_OUT,
  TIME_OUT - TIME_IN

 Sample Data 
  TIME_IN = 7:00
  TIME_OUT = 12:00
  12:00 - 7:00
my expected output is 5:00

也尝试使用DATEDIFF,但我的结果不是时间格式

DATEDIFF (HOUR, TIME_IN, TIME_OUT)

2 个答案:

答案 0 :(得分:0)

DATEDIFF的结果是指定类型的整数值。因此DATEDIFF(HOUR, '07:00', '12:00')的结果是5,而不是5:00。

SQL TIME数据类型仅在一天内24小时内完成,不适用于测量持续时间。

如果由于某种原因你想要将其格式化为时间,你需要自己做。例如,通过获取分钟数的差异,然后以小时和分钟计算正确的表示。

答案 1 :(得分:0)

这样的事情:

SELECT
   CAST(DATEDIFF(HOUR, CAST(EXTRACT(HOUR FROM CAST(CAST(:TIME_IN as varchar(5)) as time)) || ':00' as TIME), CAST(EXTRACT(HOUR FROM CAST(CAST(:TIME_OUT as varchar(5)) as time)) || ':00' as TIME))
        || ':' ||
        DATEDIFF(MINUTE, CAST('00:' || EXTRACT(MINUTE FROM CAST(CAST(:TIME_IN as varchar(5))as time)) as TIME), CAST('00:' || EXTRACT(MINUTE FROM CAST(CAST(:TIME_OUT as varchar(5)) as time)) as TIME)) as TIME)
FROM sth