MySQL DATE_FORMAT() Function
The MySQL DATE_FORMAT() function formats a date or datetime value as specified by a format mask.
Syntax
DATE_FORMAT(datetime, format_mask)
Parameters
datetime |
Required. Specify the date or datetime value to format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format_mask |
Required. Specify the format to apply to the datetime. The following is a list of options for this parameter. It can be used in many combinations.
|
Note: In MySQL, day and month range starts from 00. It allows dates to be stored incomplete. For example: '2019-00-00'.
Return Value
Returns the formatted date or datetime value as specified by a format mask.
Example 1:
The example below shows the usage of DATE_FORMAT() function.
mysql> SELECT DATE_FORMAT('2018-08-18', '%Y'); Result: '2018' mysql> SELECT DATE_FORMAT('2018-08-18', '%M %d, %Y'); Result: 'August 18, 2018' mysql> SELECT DATE_FORMAT('2018-08-18', '%M %e %Y'); Result: 'August 18 2018' mysql> SELECT DATE_FORMAT('2018-08-18', '%W, %M %e, %Y'); Result: 'Saturday, August 18, 2018' mysql> SELECT DATE_FORMAT('2018-08-18', '%W'); Result: 'Saturday'
Example 2:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Date_of_Joining |
---|---|---|---|---|
1 | John | London | 25 | 2018-05-25 |
2 | Marry | New York | 24 | 2018-10-15 |
3 | Jo | Paris | 27 | 2019-06-09 |
4 | Kim | Amsterdam | 30 | 2019-09-21 |
5 | Ramesh | New Delhi | 28 | 2019-10-25 |
In the query below, the DATE_FORMAT() function is used to format the date value of Date_of_Joining column:
SELECT *, DATE_FORMAT(Date_of_Joining, '%M %d, %Y') AS DATE_FORMAT_Value FROM Employee;
This will produce a result similar to:
EmpID | Name | City | Age | Date_of_Joining | DATE_FORMAT_Value |
---|---|---|---|---|---|
1 | John | London | 25 | 2018-05-25 | May 25, 2018 |
2 | Marry | New York | 24 | 2018-10-15 | October 15, 2018 |
3 | Jo | Paris | 27 | 2019-06-09 | June 9, 2019 |
4 | Kim | Amsterdam | 30 | 2019-09-21 | September 21, 2019 |
5 | Ramesh | New Delhi | 28 | 2019-10-25 | October 25, 2019 |
❮ MySQL Functions