-1

I currently have dates stored in my database (in string format) currently as mm/dd/yyyy.

I need to replace all entries as yyyy/mm/dd for sorting purposes.

To clarify, I am a PHP developer but my raw MySql isnt as strong. I was thinking of doing this by using substrings to re-arrange the date. (I tried in PHP first but it take way too long to process this much data, about a half million rows).

Am i going about this correctly? if so, how do I "loop through" the data, assigning my substr variables for the current iteration?

Note: I did try str_to_date here and it ended up nullifying half of the fields. Im not entirely sure why.

Your help is greatly appreciated.

4
  • stackoverflow.com/questions/9426993/mysql-change-date-format Commented Mar 8, 2013 at 17:15
  • you are looking for insert or update or select statment ? Commented Mar 8, 2013 at 17:16
  • And here we have the exact reason why the DATE and DATETIME data types exist. Commented Mar 8, 2013 at 17:22
  • To clarify, i am looking for an Update statement (sorry i should have said so) im also importing millions of rows from .CSV files with variable fields so importing as DATE was not an option here unfortunately. Commented Mar 8, 2013 at 17:31

2 Answers 2

1

this is an insert statment

  INSERT INTO yourtable (datefield) VALUES (str_to_date(date, '%Y/%m/%d'));

and this an update

 UPDATE Table SET date=STR_TO_DATE('date','%Y/%m/%d')

try this

   UPDATE Table SET date = DATE_FORMAT('date', %Y/%m/$d);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your update statement exactly as written and all rows are now NULL for that field. Ideas?
look my updated answer
It is still nullifying the data. Very strange. But Matts answer below has worked perfectly so I can just go with that.
0

I believe this will work if your column is in a consistent mm/dd/yyyy format

UPDATE mytable
SET date = CONCAT(right(date,4), '/', left(date,5))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.