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')
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')
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!