0

I have a database with several hundred fields but my data structure is wrong. It is currently in uk format as follows:

d/m /y
01/01/85
01/01/96
23/12/87

What would be the most efficient way to change the dates in bulk to sql standard of year/month/day

eg.
02/01/85 --> 1985/01/02
Yobo The Great
  • 95
  • 1
  • 13
  • possible duplicate of [Change date format in mySql database table](http://stackoverflow.com/questions/20046978/change-date-format-in-mysql-database-table) – Saty Apr 04 '15 at 11:34

2 Answers2

1
  1. Create a new DATE type column in your table:

    ALTER TABLE myTable ADD newColumn DATE;
    
  2. Use MySQL's STR_TO_DATE() function to parse the existing values into your new column:

    UPDATE myTable SET newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
    
  3. Change your codebase to use the new column. If this can't happen immediately, you could define BEFORE INSERT and BEFORE UPDATE triggers to keep the new column consistent with the old one:

    CREATE TRIGGER myTable_bi BEFORE INSERT ON myTable FOR EACH ROW
      SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
    
    CREATE TRIGGER myTable_bu BEFORE UPDATE ON myTable FOR EACH ROW
      SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
    
  4. Drop the old column:

    ALTER TABLE myTable DROP oldColumn;
    
eggyal
  • 122,705
  • 18
  • 212
  • 237
0

select date_format(date, '%Y-%m-%d') use this to change it to required format.I have used date_format function. You can get more information about date_format here

user2736738
  • 30,591
  • 5
  • 42
  • 56