×

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 FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 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 PERIOD_ADD() function

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

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL CONCAT() function

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

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 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 Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.