Change date format in MYSQL

Sometimes the situation may demand to change the MYSQL date format, when we are using select query.

MYSQL has builtin functions for this purpose, take a look at example below

SELECT DATE_FORMAT(yourColumnName, "%d/%m/%Y %H:%i") FROM yourTableName WHERE yourCondition;

From the above example, the date format of your column will be changed as per what you have mentioned (18/05/1993 19:20). You can play around with the format to achieve results as per your need. For example the format can be changed as follows.

From

"%d/%m/%Y %H:%i"

To

"%Y-%m-%d %H:%i"

Now the format will be as follows 18-05-1993 19:20.

You can use the following options too

%Y = year in 4 digit (example : 2020)

%y = year in last 2 digit (example : 20)

%m = month in 2 digit (example : 05)

%M = month in words (example : may)

%d = day in 2 digit (example : 01)

%D = day in words (example : Monday)

%H = hour in 24HR format

%h= hour in 12HR format

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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