Convert mysql data dd / mm / yyyy to yyyy-mm-dd
I have a database where I would insert the dates in the format dd/mm/yyyy
(example: 05/11/1987
), but now I am organizing some reports and I need to update all the dates in the database to the format yyyy-mm-dd
(1987-11-05
). How do I do this in MySQL?
Remembering that this is not a duplicate as I am wanting to update the data in the database and not select them in another format.
3
Author: Victor Stafusa, 2015-06-30
2 answers
Cristiano , here what you need:
UPDATE tbl_data SET data =
DATE_FORMAT(STR_TO_DATE(data, '%d/%m/%Y'), '%Y-%m-%d')
WHERE data LIKE '__/__/____'
Before
23/11/1987
After
1987-11-23
9
Author: Felipe Douradinho, 2015-06-30 16:53:24
You can make a string turn a date only with the functions available in mysql like str_to_time() and then use date_format() to change the format.
SELECT date_format(str_to_date('30/01/2015', '%d/%m/%Y'), '%Y-%m-%d')
2
Author: rray, 2015-06-30 16:48:17