Home > Database > Mysql Tutorial > How Can I Convert dd/mm/yyyy Date Strings to yyyy-mm-dd Format in MySQL?

How Can I Convert dd/mm/yyyy Date Strings to yyyy-mm-dd Format in MySQL?

Linda Hamilton
Release: 2024-12-07 22:24:17
Original
605 people have browsed it

How Can I Convert dd/mm/yyyy Date Strings to yyyy-mm-dd Format in MySQL?

Converting MySQL Date Strings to the yyyy-mm-dd Format

In certain scenarios, it becomes necessary to convert date strings stored in the dd/mm/yyyy format to the yyyy-mm-dd format for further processing. MySQL offers a straightforward solution to achieve this conversion.

Using the STR_TO_DATE() function, you can convert the date string into a datetime datatype. To specify the input format, use the corresponding format string as the second argument. In this case, it would be '%d/%m/%Y'.

STR_TO_DATE(t.datestring, '%d/%m/%Y')
Copy after login

This will return the date in a datetime data type. To ensure the desired output format, you can employ the DATE_FORMAT() function.

DATE_FORMAT(STR_TO_DATE(t.datestring, '%d/%m/%Y'), '%Y-%m-%d')
Copy after login

Alternatively, if modifying the original column's data type is not permissible, consider creating a view using the STR_TO_DATE() function to convert the string to a DateTime data type. This will provide a convenient way to access the converted dates without altering the original table.

The above is the detailed content of How Can I Convert dd/mm/yyyy Date Strings to yyyy-mm-dd Format in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template