Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

MySQL

MySQL DATE_FORMAT()

In SQL language, DATE_FORMAT() function allows formatting a DATE data in the indicated format. It is the ideal function if you want to define the date formatting directly from the SQL language.
 

Syntax:

DATE_FORMAT() function is used with the following syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT DATE_FORMAT(date, format)
SELECT DATE_FORMAT(date, format)
SELECT DATE_FORMAT(date, format)

The first parameter is DATE (or DATETIME) data type, while the second parameter is a string containing the formatting choice:

  • %a: day of the week (Sun, Mon, … Sat)
  • %b: month (Jan, Feb, … Dec)
  • %c: month number (0, 1, 2, … 12) (numeric)
  • %D: day number, with suffix (0th, 1st, 2nd, 3rd, …)
  • %d: day number of the month, with 2 decimals (00..31) (numeric)
  • %e: day number of the month (0..31) (numeric)
  • %f: microseconds (000000..999999)
  • %H: hour (00..23)
  • %h: time (01..12)
  • %I: hour (01..12)
  •  
    [st_adsense]  

  • %i: minutes (00..59) (numeric)
  • %j: day of the year (001..366)
  • %k: hour (0..23)
  • %l: hour (1..12)
  • %M: month name (January..December)
  • %m: month (00..12) (numeric)
  • %p: AM or PM
  • %r: time in 12-hour format (hh:mm:ss followed by AM or PM)
  • %S: seconds (00..59)
  • %s: seconds (00..59)
  • %T: time in 24-hour format (hh:mm:ss)
  • %U: Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
  • %u: Week (00..53), where Monday is the first day of the week; WEEK() mode 1
  • %V: Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
  • %v: Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
  • %W: day name of the week (Sunday, Monday, … Saturday)
  • %w: day number of the week (0=Sunday, 1=Monday, … 6=Saturday)
  • %X: week number of the year, for which Sunday is the first day of the week, numeric, 4 digits; used with %V
  • %x : week number of the year, for which Monday is the first day of the week, numeric, 4 digits; used with %V
  • %Y: year, numeric (with 4 digits)
  • %y: year, numeric (with 2 digits)
  • %%: one character %.
  • %x x, for any “x” not listed above

 
[st_adsense]  

Example :

It is possible to combine several values below in order to get the date formatting in the format of your choice. Here is a list of examples using DATE_FORMAT() function in SQL queries:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT DATE_FORMAT("2017-09-12", "%D %b %Y");
-- output: "12th Sep 2017"
SELECT DATE_FORMAT("2017-09-12", "%M %d %Y");
-- output: "September 12 2017"
SELECT DATE_FORMAT("2017-09-12 02:21:20", "%W %M %e %Y");
-- output: "Monday September 12 2017"
SELECT DATE_FORMAT("2017-09-12", "%d/%m/%Y");
-- output: "12/09/2017"
SELECT DATE_FORMAT("2017-09-12", "Today is : %d/%m/%Y");
-- output: "Today is : 12/09/2017"
SELECT DATE_FORMAT("2017-09-12 02:21:20", "%H:%i:%s");
-- output: "02:21:20"
SELECT DATE_FORMAT("2017-09-12", "%D %b %Y"); -- output: "12th Sep 2017" SELECT DATE_FORMAT("2017-09-12", "%M %d %Y"); -- output: "September 12 2017" SELECT DATE_FORMAT("2017-09-12 02:21:20", "%W %M %e %Y"); -- output: "Monday September 12 2017" SELECT DATE_FORMAT("2017-09-12", "%d/%m/%Y"); -- output: "12/09/2017" SELECT DATE_FORMAT("2017-09-12", "Today is : %d/%m/%Y"); -- output: "Today is : 12/09/2017" SELECT DATE_FORMAT("2017-09-12 02:21:20", "%H:%i:%s"); -- output: "02:21:20"
SELECT DATE_FORMAT("2017-09-12", "%D %b %Y");
-- output: "12th Sep 2017"
SELECT DATE_FORMAT("2017-09-12", "%M %d %Y");
-- output: "September 12 2017"
SELECT DATE_FORMAT("2017-09-12 02:21:20", "%W %M %e %Y");
-- output: "Monday September 12 2017"
SELECT DATE_FORMAT("2017-09-12", "%d/%m/%Y");
-- output: "12/09/2017"
SELECT DATE_FORMAT("2017-09-12", "Today is : %d/%m/%Y");
-- output: "Today is : 12/09/2017"
SELECT DATE_FORMAT("2017-09-12 02:21:20", "%H:%i:%s");
-- output: "02:21:20"

 
[st_adsense]  
The examples above are just a few use cases, the advantage of the DATE_FORMAT() function is that it is possible to combine the formatting in any way you like.

Example of use in a real SQL query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT *, DATE_FORMAT(registration_date, "%d/%m/%Y")
FROM users;
SELECT *, DATE_FORMAT(registration_date, "%d/%m/%Y") FROM users;
SELECT *, DATE_FORMAT(registration_date, "%d/%m/%Y")
FROM users;

This query provides a list of users, as well as the date of registration with day/month/year formatting ready to be used displayed in a format that is easy to read for users.
mcq

Leave a Reply

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