Time difference returning integer ansi sql 92

冷暖自知 提交于 2019-12-12 01:53:52

问题


I am trying to calculate the number of minutes between two datetimes using sql ansi-92. I'm not able to use timestampdiff() function because I get an error response.

I tried date1-date2, but I always get a result of 0 (although no error).

Example:

select '2016-11-02 15:25:00' - '2016-11-02 15:05:00'

回答1:


That certainly should work for you:

select TIMESTAMPDIFF(MINUTE,a.my_date2,a.my_date) diff,
       TIMESTAMPDIFF(MINUTE,'2016-11-02 15:05:00','2016-11-02 15:25:00') diff2
from my_tab a

I got the output (difference in minutes):

| diff | diff2 |
|------|-------|
|   20 |    20 |

I used this code to create table if you need it:

create table my_tab
(
  my_date datetime,
  my_date2 datetime
  )
 ;
 insert into my_tab (my_date, my_date2)
 values ('2016-11-02 15:25:00', '2016-11-02 15:05:00')
 ;


来源:https://stackoverflow.com/questions/40405224/time-difference-returning-integer-ansi-sql-92

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!