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, 1109🔥, 0💬
Popular Posts:
How To View Data Files in the Current Database in Oracle? If you want to get a list of all tablespac...
How To Start the Command-Line SQL*Plus in Oracle? If you Oracle server or client installed on your w...
What Are Out-of-Range Errors with DATETIME values in SQL Server Transact-SQL? When you enter DATETIM...
How To Generate CREATE VIEW Script on an Existing View in SQL Server? If you want to know how an exi...
What Happens If the UPDATE Subquery Returns Multiple Rows in MySQL? If a subquery is used in a UPDAT...