MySQL String to Date Conversion

date formatMySQL

I got error when i try to change string to date.

mysql> select STR_TO_DATE('03.05.2010 00:00','%d.%m.%Y %h:%i');
+--------------------------------------------------+
| STR_TO_DATE('03.05.2010 00:00','%d.%m.%Y %h:%i') |
+--------------------------------------------------+
| NULL                                             |
+--------------------------------------------------+
1 row in set, 2 warnings (0,00 sec)

how can i convert to date from '03.05.2010 00:00' ? Thanks

Best Answer

Use %H instead of %h.

select STR_TO_DATE('03.05.2010 00:00','%d.%m.%Y %H:%i');

Or, if HH:MM are always 00:00 you can use date part only:

select STR_TO_DATE('03.05.2010 00:00','%d.%m.%Y');

Check it here: http://rextester.com/CCPKR29919

As @a_vlad has pointed out on his comments, %H is used to format hours as 00..23 and %h as 01..12.

Have a look at DATE_FORMAT() on MySql docs.