1

I try to insert an csv file into mysql table. And one of the column in the file have date format like this: 3/15/2013 17:41:38 . Is it possible to insert the column, and change the date format become like this: 2013-03-15 17:41:38 ?

Thanks before

2

1 Answer 1

2

You can use STR_TO_DATE for your insert, so you can put:

STR_TO_DATE( '3/15/2013 17:41:38', '%c/%e/%Y %H:%i:%s' )

as your date column.

The format reffers to the format of the input string, and the output format will be the standard MySQL date format as you need.

The output should be as:

mysql> select STR_TO_DATE( '3/15/2013 17:41:38', '%c/%e/%Y %H:%i:%s' );
+----------------------------------------------------------+
| STR_TO_DATE( '3/15/2013 17:41:38', '%c/%e/%Y %H:%i:%s' ) |
+----------------------------------------------------------+
| 2013-03-15 17:41:38                                      |
+----------------------------------------------------------+
1 row in set (0.08 sec)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but why when i use it the result give NULL value.
It works for me: mysql> select STR_TO_DATE( '3/15/2013 17:41:38', '%c/%e/%Y %H:%i:%s' ); +----------------------------------------------------------+ | STR_TO_DATE( '3/15/2013 17:41:38', '%c/%e/%Y %H:%i:%s' ) | +----------------------------------------------------------+ | 2013-03-15 17:41:38 | +----------------------------------------------------------+ 1 row in set (0.08 sec) (Sorry, I can't properly format the output on a comment.
@Jester don't comment, click "edit" on your answer to update it with more information.
@thenoirlatte If the solution worked for you, accept it as correct and upvote it please. ;)
i'm sorry, my reputation not enough to vote up, it needs 15 reputation :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.