Format Date in MySQL with DATE_FORMAT()

MySQL support datetime values into different formats using the DATE_FORMAT() function. This function accepts date or datetime values as a first parameter and returns to a specific format defined in the second parameter.

Define a DATETIME variable
SET @date:='2014-06-16 14:12:49';

-- Display datetime values in YYYY-mm-dd format
SELECT date_format(@date,'%Y-%m-%d') AS formatted_date;

This would return: Monday, June 16, 2014

— Display datetime values in Month Year format
SELECT date_format(@date,‘%M %Y’)  AS formatted_date;

This would return June 2014.

— Display datetime values in HH:MM:SS format
SELECT date_format(@date,‘%T’) AS formatted_date;

This would return 14:12:49.

-- Display datetime values as Full date format
SELECT date_format(@date,'%W, %M %d,%Y %T') ASformatted_date;

This would return Monday, June 16,2014 14:12:49


Read the rest of the post here:

Leave a Reply

Your email address will not be published. Required fields are marked *