I have the string 04/04/2012 04:03:35 AM
. How can I convert it and insert into a table? Field type is datetime
.
Asked
Active
Viewed 3.4k times
12

ballade4op52
- 2,142
- 5
- 27
- 42

Anton Sementsov
- 1,196
- 6
- 18
- 34
-
1it`s return empty string – Anton Sementsov May 10 '12 at 17:23
-
`cast ('04/04/2012 04:03:35 AM') as datetime`? – Lev Levitsky May 10 '12 at 17:24
-
Post the Insert SQL you are using. – Churk May 10 '12 at 17:25
-
Downvoters care to explain? It's a perfectly valid question. – eggyal May 10 '12 at 17:38
-
@eggyal Please refer to the http://stackoverflow.com/questions/how-to-ask, i am sure there is a clause there that say provide example of what you have tried first and make sure you search for your answer prior to asking, such as http://stackoverflow.com/questions/3296725/parse-date-in-mysql – Churk May 10 '12 at 22:11
-
@Churk: And yet you commented first, with an unhelpful and incorrect response; surely better to have said then exactly what you just did to me? – eggyal May 10 '12 at 22:30
1 Answers
21
You need to use STR_TO_DATE()
:
STR_TO_DATE('04/04/2012 04:03:35 AM', '%d/%m/%Y %r')
Or, better yet, present your literal in a format MySQL expects (e.g. YYYY-MM-DD HH:mm
).

eggyal
- 122,705
- 18
- 212
- 237