Home > Enterprise >  MySQL TIMESTAMPDIFF returns 0 even though there is 1 hour difference between two values
MySQL TIMESTAMPDIFF returns 0 even though there is 1 hour difference between two values

Time:11-04

I feel like I am doing something wrong but I cannot understand what.

Let's say I have a query:

select timestampdiff(minute, '2022-10-31t11:25:00.000', '2022-10-31t12:25:00.000')

It returns 0 but it should return 60, no? There is a 1 hour difference between the two. It returns anything other than 0 only if the date is different. What is wrong here?

CodePudding user response:

Your query is not using valid MySQL timestamp literals. If you want a T separator between the date and time portions of the timestamps, it must be uppercase. From the MySQL documentation:

The date and time parts can be separated by T rather than a space. For example, '2012-12-31 11:30:45' '2012-12-31T11:30:45' are equivalent.

SELECT TIMESTAMPDIFF(minute,
                     '2022-10-31T11:25:00.000',
                     '2022-10-31T12:25:00.000');  -- 60
  • Related