In MS SQL Server, FORMAT is used to format both numeric values and date/time values to strings. In MySQL, you use FORMAT for numeric values and DATE_FORMAT for date and time values.
The FORMAT function was introduced in MS SQL Server 2012 only.
Syntax:
FORMAT ( value, format [, culture ] )
SELECT FORMAT(123456789,'###-##-####') AS 'Custom Number Result';
----------------
123-45-6789
SELECT FORMAT(1.2365, 'N', 'en-us') AS 'Number Format'
,FORMAT(1.2365, 'G', 'en-us') AS 'General Format'
,FORMAT(1.2365, 'C', 'en-us') AS 'Currency Format'
Number Format General Format Currency Format
------------- -------------- ---------------
1.24 1.2365 $1.24
SELECT FORMAT ( '02/08/2002', 'D', 'en-US' ) AS 'US English Result'
----------------
Friday, February 8, 2002
SELECT FORMAT ( '02/08/2002', 'D', 'en-gb' ) AS 'Great Britain English Result'
----------------
08 February 2002
In MySQL, use FORMAT to format numbers and DATE_FORMAT to format dates.
Syntax:
FORMAT(X, D [, locale])
Formats the number X to a format like '#,###,###.##', rounded to D decimal places, returned as a string. If D is 0, the result has no decimal point or fractional part.
mysql> select format(123.4567,3);
+--------------------+
| format(123.4567,3) |
+--------------------+
| 123.457 |
+--------------------+
mysql> select format(123.4567,1);
+--------------------+
| format(123.4567,1) |
+--------------------+
| 123.5 |
+--------------------+
mysql> select format(12345.6789,5);
+----------------------+
| format(12345.6789,5) |
+----------------------+
| 12,345.67890 |
+----------------------+
mysql> select format(12345.6789,0);
+----------------------+
| format(12345.6789,0) |
+----------------------+
| 12,346 |
+----------------------+
DATE_FORMAT has to be used in MySQL to format dates. It provides many formatting symbols to convert dates into any format you like.
Syntax:
DATE_FORMAT(date, format)
Common format symbols:
| Symbol | Description |
|---|---|
%a | Abbreviated weekday name (Sun..Sat) |
%b | Abbreviated month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%D | Day of month with English suffix (0th, 1st, 2nd, 3rd, ...) |
%d | Day of month, numeric (00..31) |
%e | Day of month, numeric (0..31) |
%f | Microseconds (000000..999999) |
%H | Hour (00..23) |
%h | 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 / %s | Seconds (00..59) |
%T | Time, 24-hour (hh:mm:ss) |
%W | Weekday name (Sunday..Saturday) |
%w | Day of week (0=Sunday..6=Saturday) |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal % character |
mysql> SELECT DATE_FORMAT(CURRENT_DATE,'%W %D %M %Y ');
+------------------------------------------+
| DATE_FORMAT(CURRENT_DATE,'%W %D %M %Y ') |
+------------------------------------------+
| Tuesday 19th July 2016 |
+------------------------------------------+
mysql> SELECT DATE_FORMAT(CURRENT_DATE,'%d-%m-%y');
+--------------------------------------+
| DATE_FORMAT(CURRENT_DATE,'%d-%m-%y') |
+--------------------------------------+
| 19-07-16 |
+--------------------------------------+
mysql> SELECT DATE_FORMAT(CURRENT_DATE,'%d-%b-%y');
+--------------------------------------+
| DATE_FORMAT(CURRENT_DATE,'%d-%b-%y') |
+--------------------------------------+
| 19-Jul-16 |
+--------------------------------------+
