0

I'm trying to format a string containing a time-value with the STR_TO_DATE function. The time value is : hour(0-23) without leading zero + minute (00-59) e.g. 800, 1245 or 1535. To format these values I tried to use the %k hour specifier as described in the MySQL5.5 documentation, but this does not work if the hour is without a leading zero:

Does not work:

+-----------------------------------+
| SELECT STR_TO_DATE("800","%k%i"); |
+-----------------------------------+
| NULL                              |
+-----------------------------------+

Works as expected:

+------------------------------------+
| SELECT STR_TO_DATE("1855","%k%i"); |
+------------------------------------+
| 18:55:00                           |
+------------------------------------+

+-----------------------------------+
| SELECT STR_TO_DATE("0800","%k%i");|
+-----------------------------------+
| 08:00:00                          |
+-----------------------------------+
Michael
  • 116
  • 6

1 Answers1

1

You can use LPAD() to add a leading zero:

SELECT STR_TO_DATE(LPAD("800", 4, 0), "%k%i");
Paul Spiegel
  • 30,925
  • 5
  • 44
  • 53