Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to datetime in my sql [duplicate]

Tags:

mysql

I want to convert the string "2017-03-13T14:39:00.000" into datetime format in my sql select query.

After converting it, I have to compare this with another date. So I'm expecting the converted datetime should be compatible with comparison operators (< and >).

Thanks

like image 597
Santhosh Avatar asked Jun 28 '17 12:06

Santhosh


1 Answers

You might want to look at the STR_TO_DATE() function of mysql.

Example (added)

SELECT STR_TO_DATE('2017-03-13T14:39:01.123','%Y-%m-%dT%H:%i:%s.%f');

This is my result

+---------------------------------------------------------------+                                                                                        
| STR_TO_DATE('2017-03-13T14:39:01.123','%Y-%m-%dT%H:%i:%s.%f') |                                                                                        
+---------------------------------------------------------------+                                                                                        
| 2017-03-13 14:39:01.123000                                    |                                                                                        
+---------------------------------------------------------------+                                                                                        
1 row in set (0,00 sec)                                                                                                                                  
mysql>
like image 187
mbieren Avatar answered Nov 03 '22 08:11

mbieren