×

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples.

Introduction of MySQL DATE_FORMAT() function

DATE_FORMAT() function in MySQL is used to format a specified date as a given format value, i.e., a date will be given, and this function will format that date as specified format parameters.

Syntax of the MySQL DATE_FORMAT() function

The syntax of the MySQL DATE_FORMAT() function is given as follows:

DATE_FORMAT(dt, Sformat)

Parameters or arguments used in MySQL DATE_FORMAT() function:

There are two parameters accepted by the DATE_FORMAT() function in MySQL, which are given as follows:

Dt: it is the mentioned date to be formatted.

Sformat: it is the mentioned format. This list of formats used in this function are listed below:

Format Description of MySQL DATE_FORMAT function:

%a: This abbreviation means weekday name. Its limit is from Sun to Sat.

%b: This abbreviation means month name. Its limit is from Jan to Dec.

%c: This abbreviation means numeric month name. Its limit is from 0 to 12.

%D: This abbreviation means a day of the month as a numeric value, followed by a suffix like 1st, 2nd, etc.

%e: This abbreviation means a day of the month as a numeric value. Its limit is from 0 to 31.

%f: This abbreviation means microseconds. Its limit is from 000000 to 999999.

%H: This abbreviation means hour. Its limit is from 00 to 23.

%I: This abbreviation means minutes. Its limit is from 00 to 59.

%j: This abbreviation means the day of the year. Its limit is from 001 to 366.

%M: This abbreviation means month name from January to December.

%p: This abbreviation means AM or PM.

%S: This abbreviation means seconds. Its limit is from 00 to 59.

%U: This abbreviation means week, where Sunday is the first day of the week. Its limit is from 00 to 53.

%W: This abbreviation means weekday names from Sunday to Saturday.

%Y: This abbreviation means year as a numeric value of 4 digits.

Returns :

It will return the formatted date.

Example 1:

In this example, we will get a formatted year as “2020” from the specified date “2020-11-23” in MySQL.

SELECT DATE_FORMAT("2020-11-23", "%Y");

Output:

2020
1 row in set (0.00 sec)

Example 2:

In this example, we will get a formatted month name as “December” from the specified date “2020-12-23” in MySQL.

SELECT DATE_FORMAT("2020-12-23", "%M");

Output:

December
1 row in set (0.00 sec)

Example 3:

In this example, we will get a day of the month as a numeric value as “23rd” from the specified date “2020-11-23” in MySQL.

SELECT DATE_FORMAT("2020-11-23", "%D");

Output:

23rd
1 row in set (0.00 sec)

Example-4 :

In this example, we will get the month, day, and year as “December 23, 2020” from the specified date “2020-12-23” in MySQL.

SELECT DATE_FORMAT("2020-12-23", "%M %d %Y");

Output:

December 23 2020
1 row in set (0.00 sec)

Example 5:

In this example, we will get hour and minute as “12 09” from the specified date and time “2020-11-23 12:09:23”.

SELECT DATE_FORMAT("2020-11-23 12:09:23", "%H %i");

Output:

12 09
1 row in set (0.00 sec)

Application of MySQL DATE_FORMAT() function:

This function is used to format a specified date as a given format value, i.e., a date will be given, and this function will format it.

Summary:

In the above context, we have learned how to use the DATE_FORMAT() function in MySQL to format a specified date as a given format value, i.e., a date will be given, and this function will format it.


Related Topics

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL Character Length Function

In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples. Introduction of MySQL CHARACTER_LENGTH() function CHARACTER_LENGTH() function in MySQL is used to...

2 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.