EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL DATE_FORMAT()
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL DATE_FORMAT()

By Aanchal SharmaAanchal Sharma

MySQL DATE_FORMAT()

Introduction to MySQL DATE_FORMAT()

MySQL DATE_FORMAT() function is a useful tool in MySQL to design a Date according to the indicated format provided in the query statement to retrieve the date value as output. Simply, the MySQL DATE_FORMAT() function presents the Date value to a given layout or design that it accepts as arguments while executing. Here, the format is a type of string arrangement that includes some predefined specifiers in MySQL where % is a required character to be placed before each specifier. Also, the resultant of MySQL DATE_FORMAT() function is a string but the character set and collation of it, is based on the settings of the client’s connection.

Syntax of MySQL DATE_FORMAT()

The succeeding syntax specifies the structure to write query for MySQL DATE_FORMAT() function:

DATE_FORMAT(Date_Value,Format);

In additional to this syntax, we use SELECT keyword with this MySQL function to execute a query statement like this:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

SELECT DATE_FORMAT(Date_Value,Format);

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

Hence, the MySQL DATE_FORMAT() function takes two parameters as inputs to provide the required date format structure. Date_Value is the argument that denotes the valid date value that needs to be formatted and format is the required parameter to define the arrangement or format to be used for the Date_Value.

The format is constructed using the predefined specifiers which results a string type.

So, we can illustrate the format having one or multiple grouping of the below values:

Format

Explanation

%a Everyday name in Shortened form [Sun–Sat].
%b Month name in Shortened form [Jan – Dec].
%c Name of Month in numbers [0 – 12].
%D Daytime of the month in a numeric form, trailed by suffix [like 1st, 2nd, …].
%d Daytime of the month as a numeric form[01 – 31].
%e Daytime of the month as a numeric form [0 – 31].
%f Denotes Microseconds within range [000000 – 999999].
%H Denotes Hour within range[00 – 23].
%h Denotes Hour within range [00 – 12].
%I Denotes Hour within range [00 – 12].
%i Numeric Minutes ranging [00 – 59].
%j Daytime of the year [001 – 366].
%k Denotes Hour within range [0 – 23].
%l Denotes Hour within range [1 – 12].
%M Full name of month [January – December].
%m Numeric name of Month [00 – 12].
%p Either AM or PM.
%r Denotes Time in 12 hour AM or PM format [hh:mm:ss AM/PM].
%S Denotes Seconds ranging [00 – 59].
%s Denotes Seconds ranging [00 – 59].
%T 24 hour format of Time [hh:mm:ss].
%U Denotes Week where Sun is the initial day of the week [00 – 53].
%u Denotes Week where Mon is the initial day of the week [00 – 53].
%V Denotes Week where Sun is the initial day of the week [01 – 53] and used also with %X.
%v Denotes Week where Mon is the initial day of the week [01 – 53] and used also with %X.
%W Name of Weekday in full [Sunday – Saturday].
%w Daytime of the week where Sunday=0 and Saturday=6.
%X Denotes Year for the week where Sun is the initial day of the week and used also with %V.
%x Denotes Year for the week where Mon is the first day of the week and used also with %V.
%Y Denotes Year as a numeric, 4-digit value.
%y Denotes Year as a numeric, 2-digit value.
%% Supplements percentage (%) character to the result.

How MySQL DATE_FORMAT() Function work?

MySQL DATE_FORMAT() function works using the following query structure:

Code:

SELECT DATE_FORMAT(Date_Value, ‘%a  %D  %b  %Y’) as Date_Formatted FROM TableName;

In this above query, we see that the Date_Value represents a particular value or any table column value with Date as Data type with alias as Date_Formatted that denotes the column title in the output. The result string will be set for the specified format as ‘%a %D %b %Y’, which includes the weekend name in abbreviated form, name of the day of the month with English suffix, month name in abbreviated form and four digits year value in number form.

Examples of MySQL DATE_FORMAT()

Given below are the examples of MySQL DATE_FORMAT():

Example #1

Some simple examples using DATE_FORMAT() function.

Some DATE_FORMAT() function examples are written with following SQL statements:

Code:

SELECT DATE_FORMAT('2020-05-20','%a  %b  %Y');

This results the day and month with year as specified in the date provided in the DATE_FORMAT() function.

Output:

MySQL DATE_FORMAT() 1

Code:

SELECT DATE_FORMAT('2020-05-20','%M  %D  %Y');

Output:

('2020-05-20','%M  %D  %Y');

Code:

SELECT DATE_FORMAT('2020-05-20','%W %D');

Output:

MySQL DATE_FORMAT() 3

The following query includes date with time value also:

Code:

SELECT DATE_FORMAT('2020-05-20 21:40:15', '%W %D %M %Y%r');

Output:

MySQL DATE_FORMAT() 4

The resultant format of the above query displays the time value extracted from the given date value in the argument of the function above.

Code:

SELECT DATE_FORMAT('2020-05-20 21:40:15', '%r %f ');

Output:

MySQL DATE_FORMAT() 5

Here, the output has date format in Seconds.

SELECT DATE_FORMAT('2020-05-20','%c  or %b');

Output:

MySQL DATE_FORMAT() 6

The result consists of both numeric month name and shortened month name.

Example #2

Example of DATE_FORMAT() function with MySQL NOW() function.

You can also use MySQL NOW() function with the MySQL DATE_FORMAT() to access the present date/time values. This can be defined using the following commands with specifiers.

Code:

SELECT DATE_FORMAT(NOW(),'%d  %b  %y');

Output:

(NOW(),'%d  %b  %y');

Code:

SELECT DATE_FORMAT(NOW(),'%d  %b  %Y %T:%f');

Output:

MySQL DATE_FORMAT() 8

Code:

SELECT DATE_FORMAT(NOW(),'%b  %d  %y  %h : %i  %p');

Output:

'%b  %d  %y  %h : %i  %p');

Code:

SELECT DATE_FORMAT(NOW(),'%m - %d -  %Y');

Output:

'%m - %d -  %Y');

Example #3

Example of DATE_FORMAT() function with Table Column Value.

Suppose, we have a table in the database named Persons, then we will apply the MySQL DATE_FORMAT() function to one of the column having Date as a data type.

Code:

SELECT JoinDate AS 'Date Value', DATE_FORMAT(JoinDate,'%a  %M  %Y') AS 'Formatted_Date' FROM Persons;

Output:

with Table Column Value

From the above query, we came to know that the JoinDate which is a column in the table Persons and using the DATE_FORMAT() function we are displaying the date values in a specified format.

Conclusion

The MySQL DATE_FORMAT() function defines the arrangement to display the Date/Time value in several formats defined by the specifiers in MySQL. Thus, the MySQL DATE_FORMAT() function allows to provide date value as 2020-05-27 to Tuesday, May 2020. We can define the required formats whatever using other specifiers with combinations to make the date layout proper.

Recommended Articles

This is a guide to MySQL DATE_FORMAT(). Here we discuss how MySQL DATE_FORMAT() function work? and examples respectively. You may also have a look at the following articles to learn more –

  1. MySQL Administration
  2. MySQL Database Repair
  3. MySQL Timestamp
  4. MySQL Constraints
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more