Skip to content

Instantly share code, notes, and snippets.

@brantfaircloth
Last active December 21, 2015 15:59
Show Gist options
  • Save brantfaircloth/6330366 to your computer and use it in GitHub Desktop.
Save brantfaircloth/6330366 to your computer and use it in GitHub Desktop.
converting mysql date and time strings to date and time values
mysql> select usdi, capture_date, time,
-> str_to_date(concat(capture_date, ' ', time), '%m/%d/%y %k%i')
-> as str_to_datetime
-> from all_bands_temp order by RAND() limit 10;
+-----------+--------------+------+---------------------+
| usdi | capture_date | time | str_to_datetime |
+-----------+--------------+------+---------------------+
| 139106995 | 6/12/91 | 0707 | 1991-06-12 07:07:00 |
| 149143944 | 6/2/95 | 1212 | 1995-06-02 12:12:00 |
| 139107786 | 5/12/90 | 1730 | 1990-05-12 17:30:00 |
| 147164369 | 5/24/91 | 1030 | 1991-05-24 10:30:00 |
| 149143925 | 5/28/95 | 1140 | 1995-05-28 11:40:00 |
| 154189201 | 5/5/02 | 1338 | 2002-05-05 13:38:00 |
| 123156987 | NULL | NULL | NULL |
| 190134380 | 6/11/04 | 0000 | 2004-06-11 00:00:00 |
| 139109340 | 7/10/87 | 0830 | 1987-07-10 08:30:00 |
| 154189917 | 5/3/03 | 1332 | 2003-05-03 13:32:00 |
+-----------+--------------+------+---------------------+
mysql> select usdi, capture_date, time from all_bands_temp
-> order by RAND() limit 10;
+-----------+--------------+------+
| usdi | capture_date | time |
+-----------+--------------+------+
| 154187574 | 5/7/98 | 1312 |
| 139107796 | 5/14/90 | 0820 |
| 136141001 | 5/2/85 | 1209 |
| 187140505 | 5/31/05 | 0750 |
| 190134590 | 5/9/06 | 0000 |
| 190134700 | 7/10/06 | 0000 |
| 139105075 | 4/28/88 | 1135 |
| 139107358 | 8/11/89 | 1017 |
| 190134254 | 4/29/04 | 0000 |
| 154189988 | 5/20/03 | 1322 |
+-----------+--------------+------+
10 rows in set (0.12 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment