MySQL DATE_FORMAT: Format Date as String
In MySQL, you can use the DATE_FORMAT function to format the date into a string.
The grammar is as follows:
SELECT DATE_FORMAT(date_column, 'format_string') FROM table_name;
In the above syntax, date_column is the name of the date column, and format_string is the string used to specify the date format. Here are some commonly used date format strings:
- The year in four digits, represented by %Y.
- %y: the year, with two digits
- %m: month, two digits
- %d: Date, two-digit number
- %H: Hours (24-hour format), two digits
- %h: hours (12-hour clock), two digits
- %i: minutes, two digits
- %s: seconds, two-digit number
For example, to format the date column as a string in the YYYY-MM-DD format, you can use the following query:
SELECT DATE_FORMAT(date_column, '%Y-%m-%d') FROM table_name;
This will allow you to format the date as a string.