In MySQL, you can use the GET_FORMAT() function to return a format string for the provided arguments. This can be useful when you need to provide a format string to another function, such as DATE_FORMAT() or STR_TO_DATE().
Syntax
The official syntax goes like this:
GET_FORMAT({DATE|TIME|DATETIME}, {'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL'})
Example 1 – Basic Usage
Here’s an example to demonstrate how it works.
SELECT GET_FORMAT(DATE,'USA');
Result:
+------------------------+ | GET_FORMAT(DATE,'USA') | +------------------------+ | %m.%d.%Y | +------------------------+
So this example returns the format string for USA. We can now take that format string and use it in various date formatting functions.
Like this:
SELECT DATE_FORMAT('2020-03-07', '%m.%d.%Y') AS 'Formatted';
Result:
+------------+ | Formatted | +------------+ | 03.07.2020 | +------------+
However, we can also pass this function directly to the DATE_FORMAT() function. Like this:
SELECT DATE_FORMAT('2020-03-07', GET_FORMAT(DATE,'USA')) AS 'Formatted';
Result:
+------------+ | Formatted | +------------+ | 03.07.2020 | +------------+
So this saves us from having to remember what format string to use – GET_FORMAT() does that for us.
Example 2 – Date Values
This example lists out all the variations of the second argument when the first argument is DATE.
SELECT
GET_FORMAT(DATE,'USA') AS 'USA',
GET_FORMAT(DATE,'JIS') AS 'JIS',
GET_FORMAT(DATE,'ISO') AS 'ISO',
GET_FORMAT(DATE,'EUR') AS 'EUR',
GET_FORMAT(DATE,'INTERNAL') AS 'INTERNAL';
Result:
+----------+----------+----------+----------+----------+ | USA | JIS | ISO | EUR | INTERNAL | +----------+----------+----------+----------+----------+ | %m.%d.%Y | %Y-%m-%d | %Y-%m-%d | %d.%m.%Y | %Y%m%d | +----------+----------+----------+----------+----------+
Example 3 – Datetime Values
This example lists out all the variations of the second argument when the first argument is DATETIME.
SELECT
GET_FORMAT(DATETIME,'USA') AS 'USA',
GET_FORMAT(DATETIME,'JIS') AS 'JIS',
GET_FORMAT(DATETIME,'ISO') AS 'ISO',
GET_FORMAT(DATETIME,'EUR') AS 'EUR',
GET_FORMAT(DATETIME,'INTERNAL') AS 'INTERNAL';
Result:
+-------------------+-------------------+-------------------+-------------------+--------------+ | USA | JIS | ISO | EUR | INTERNAL | +-------------------+-------------------+-------------------+-------------------+--------------+ | %Y-%m-%d %H.%i.%s | %Y-%m-%d %H:%i:%s | %Y-%m-%d %H:%i:%s | %Y-%m-%d %H.%i.%s | %Y%m%d%H%i%s | +-------------------+-------------------+-------------------+-------------------+--------------+
Example 4 – Time Values
This example lists out all the variations of the second argument when the first argument is TIME.
SELECT
GET_FORMAT(TIME,'USA') AS 'USA',
GET_FORMAT(TIME,'JIS') AS 'JIS',
GET_FORMAT(TIME,'ISO') AS 'ISO',
GET_FORMAT(TIME,'EUR') AS 'EUR',
GET_FORMAT(TIME,'INTERNAL') AS 'INTERNAL';
Result:
+-------------+----------+----------+----------+----------+ | USA | JIS | ISO | EUR | INTERNAL | +-------------+----------+----------+----------+----------+ | %h:%i:%s %p | %H:%i:%s | %H:%i:%s | %H.%i.%s | %H%i%s | +-------------+----------+----------+----------+----------+
Unit Specifiers
The returned format string can be made up of any combination of the following 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; WEEK() mode 0 |
%u |
Week (00..53), where Monday is the first day of the week; WEEK() mode 1 |
%V |
Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X |
%v |
Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x |
%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, for any “x” not listed above |