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:

  1. The year in four digits, represented by %Y.
  2. %y: the year, with two digits
  3. %m: month, two digits
  4. %d: Date, two-digit number
  5. %H: Hours (24-hour format), two digits
  6. %h: hours (12-hour clock), two digits
  7. %i: minutes, two digits
  8. %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.

bannerAds