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, 1102🔥, 0💬
Popular Posts:
How Fixed Length Strings Are Truncated and Padded in SQL Server Transact-SQL? When the length of the...
How To Connect to a MySQL Server with a Port Number in MySQL? If you want to connect a MySQL server ...
How To Get the Definition of a Stored Procedure Back in SQL Server Transact-SQL? If you want get the...
How to download and install Microsoft SQL Server Management Studio Express in SQL Server? Microsoft ...
How To Connect ASP Pages to Oracle Servers in Oracle? If you are running Windows IIS Web server and ...