At the moment, we don't really know the reason why you want to change the date format, but I have a couple of assumption:
- You want to insert date data from front end into the table but couldn't do so due to the mismatched date format.
- You want the end output to show the date format of
DD/MM/YYYY
instead.
Although, it may look like you need to change the table column date format, there's a way to avoid that operation entirely. However, since you've mentioned changing from YYYY/MM/DD
in your title, I'm not sure if your date column is actually DATE
datatype because the default MySQL should be YYYY-MM-DD
. Nonetheless, I'll address the matter in this answer altogether.
Modify the date format in query and leave the table date column datatype as it is:
From DD\MM\YYYY
to MySQL DATE
datatype format YYYY-MM-DD
... STR_TO_DATE(data, '%d/%m/%Y')
.. from YYYY-MM-DD
to DD/MM/YYYY
... DATE_FORMAT(data, '%d/%m/%Y')
You can use any of that anywhere in a query; whether in SELECT
or WHERE
.
If "in query" is not what you want and you still want to update the table:
Well, you have two options here:
- Directly modify the column datatype then update the value:
ALTER TABLE mytable MODIFY COLUMN date_col VARCHAR(255);
UPDATE mytable SET date_col =DATE_FORMAT(date_col , '%d/%m/%Y');
- Or you can add another column, populate the desired date format there and keep the original date column as it is:
ALTER TABLE mytable ADD COLUMN my_date VARCHAR(255);
UPDATE mytable SET my_date =DATE_FORMAT(date_col , '%d/%m/%Y');
this way you have the option to directly use MySQL date functions on the default MySQL date column without the hassle of converting your desired date format into the default before you can use date functions. What I'm saying is something like this:
DAY(mysql_default_dateformat)
is similar to
DAY(STR_TO_DATE(your_dateformat, '%d/%m/%Y'))
which means that you can use DAY()
(date function) on the default date format directly without the need to convert what is not default first.
Here's demo fiddle examples