Home >Database >Mysql Tutorial >How Can I Convert dd/mm/yyyy Strings to MySQL Dates in SELECT Queries?

How Can I Convert dd/mm/yyyy Strings to MySQL Dates in SELECT Queries?

Susan Sarandon
Susan SarandonOriginal
2024-12-14 18:55:10546browse

How Can I Convert dd/mm/yyyy Strings to MySQL Dates in SELECT Queries?

Converting Strings to Dates in MySQL SELECT Queries

Many systems store dates as strings in the format dd/mm/yyyy. To utilize date functions like DATE_FORMAT, it's necessary to convert these strings to the MySQL-compatible format yyyy-mm-dd.

To accomplish this conversion, the STR_TO_DATE function can be employed:

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

This will return a datetime datatype. To ensure the desired format, DATE_FORMAT can be used as follows:

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

If the original column's datatype cannot be altered, it's recommended to create a view that performs the STR_TO_DATE conversion to a DateTime data type.

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

Statement:
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