1

I am trying to execute this query on MYSQL 5.5.34

SELECT `CustomerName`,`BookingDate`, HOUR(`BookingDate`)from bookings;

which results in NULL,

but same query is working correctly in MYSQL 5.5.16 and returns Hours of particular date

Any help or Thoughts of bug reporting about MYSQL 5.5.34

4
  • may you try HOUR(cast('BookingDate' as datetime))
    – revo
    Oct 22, 2013 at 7:54
  • 1
    Possible datatype error.
    – Mark
    Oct 22, 2013 at 7:58
  • could not cast to dateTime format, it also returns Null.... my date time format is '2013-9-30 17:10:169-300' Oct 22, 2013 at 8:05
  • @NaumanRafique Your datetime format is incorrect. What does 169-300 part represent?
    – peterm
    Oct 22, 2013 at 9:41

2 Answers 2

2

Your datetime format is incorrect. It's unclear what 169-300 part represents...

But you can extract an hour part even from partially correct format by using STR_TO_DATE() first

SELECT CustomerName,
       BookingDate, 
       HOUR(STR_TO_DATE(BookingDate, '%Y-%c-%e %H:%i')) hour
  FROM bookings;

Output:

| CUSTOMERNAME |             BOOKINGDATE | HOUR |
|--------------|-------------------------|------|
|    Customer1 | 2013-9-30 17:10:169-300 |   17 |

Here is SQLFiddle demo (MySQL 5.1.61)

1

HOUR() function in MySQL has an argument of time datatype. If you want to get the time from a datetime format, you should use EXTRACT() like:

-- SYNTAX: EXTRACT(unit FROM date)
SELECT `CustomerName`,`BookingDate`, EXTRACT(HOUR FROM CAST(`BookingDate` as datetime)) from bookings;

Reference:

3
  • What is the datatype of your BookingDate?
    – Mark
    Oct 22, 2013 at 8:13
  • varchar... its giving correct result in MySQL 5.5.16 with same datatype Oct 22, 2013 at 8:16
  • Even if you have a datetime format, still you have to use EXTRACT()
    – Mark
    Oct 22, 2013 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.