Time difference returning integer ansi sql 92

2019-09-15 05:40发布

问题:

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')
 ;