Collections:
DATE_FORMAT() - Formatting Date
How to convert a datetime into formatted string using the DATE_FORMAT() function?
✍: FYIcenter.com
DATE_FORMAT(date, format) is a MySQL built-in function that
converts a datetime into a formatted string.
For example:
SELECT DATE_FORMAT('2023-10-04 22:23:00', '%W %M %e, %Y');
-- +----------------------------------------------------+
-- | DATE_FORMAT('2023-10-04 22:23:00', '%W %M %e, %Y') |
-- +----------------------------------------------------+
-- | Wednesday October 4, 2023 |
-- +----------------------------------------------------+
SELECT DATE_FORMAT(NOW(3), '%H:%i:%s.%f');
-- +------------------------------------+
-- | DATE_FORMAT(NOW(3), '%H:%i:%s.%f') |
-- +------------------------------------+
-- | 15:37:15.097000 |
-- +------------------------------------+
SELECT DATE_FORMAT(NOW(6), '%Y-%m-%dT%H:%i:%s.%f');
-- +---------------------------------------------+
-- | DATE_FORMAT(NOW(6), '%Y-%m-%dT%H:%i:%s.%f') |
-- +---------------------------------------------+
-- | 2023-11-22T15:42:57.213548 |
-- +---------------------------------------------+
Reference information of the DATE_FORMAT() function:
DATE_FORMAT(datetime, format): str
Formats the datetime value according to the format string.
Arguments, return value and availability:
datetime: Required. The datetime to be formatted.
format: Required. The string with format specifiers.
str: Return value. The formatted datetime
Available since MySQL 4.
Datetime format specifiers:
Specifier Description
--------- -----------
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week
%v Week (01..53), where Monday is the first day of the week
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week,
numeric, four digits; used with %V
%x Year for the week where Monday is the first day of the week,
numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal % character
%x x, for any “x” not listed above
⇒ DATE_SUB() - Decrementing Date/Time
⇐ DATE_ADD() - Adding Date/Time Interval
2023-11-17, 1841🔥, 0💬
Popular Posts:
Where to find answers to frequently asked questions on INSERT, UPDATE and DELETE Statements in MySQL...
How To Update Multiple Rows with One UPDATE Statement in SQL Server? If the WHERE clause in an UPDAT...
What Are the Basic Features of a Trigger in SQL Server? Since a SQL Server trigger is a really an ev...
How To Use DATEADD() Function in SQL Server Transact-SQL? DATEADD() is a very useful function for ma...
Where to find Oracle database server tutorials? Here is a collection of tutorials, tips and FAQs for...