Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql HOUR() converts -HH to HH, is this a feature or a bug?

Tags:

time

mysql

If I do a query such as the following:

SELECT HOUR( TIMEDIFF('2012-08-12 02:25:00', 
                      '2012-09-14 02:25:33') ) as result

the result is 792, even though I'm subtracting a past data from a future date.

If I remove HOUR() and do:

SELECT TIMEDIFF('2012-08-12 02:25:00', 
                '2012-09-14 02:25:33')  as result

Then the value is -792:00:33. Therefore clearly HOUR() is converting -792 to 792. I've tried it with other dates and result is the same (always positive hour being returned).

The manual doesn't say anything about this behaviour.

Is this a bug, or is it a feature that I can rely on to be present on all recent mysql installations?

like image 272
Ali Avatar asked Sep 13 '12 21:09

Ali


1 Answers

There's a way to get the positive hour and the negative hour. EXTRACT will get the negative one.

SELECT EXTRACT(HOUR FROM '-01:00:00'), HOUR('-01:00:00')

Will return:

-1    1
like image 176
Korem Avatar answered Dec 15 '22 01:12

Korem