FORMAT() Function in SQL Server
The FORMAT() function is one of the String Function, which is used to format the specified value in the given format.
Syntax :
FORMAT(value, format, culture)
Parameter : This method accepts three-parameters as mentioned above and described below :
- value : It is an expression of a supported data type to format.
- format : It is the required format in which we require the output.
- culture : This optional parameter specifies a culture.
Returns : It returns a value formatted with the specified format and optional culture.
Example-1 : Format a Number.
SELECT FORMAT(25, 'N')
Output :
25.00
Example-2 : Example of PERCENTAGE format.
SELECT FORMAT(1, 'P', 'en-US')AS [PERCENTAGE IN US FORMAT], FORMAT(1, 'P', 'en-IN') AS [PERCENTAGE IN INDIA FORMAT];
Output :
PERCENTAGE IN US FORMAT | PERCENTAGE IN INDIA FORMAT |
---|---|
100.00 % | 100.00 % |
Example-3 : Example of DATE format.
DECLARE @d DATETIME = GETDATE(); SELECT FORMAT( @d, 'dd/MM/yyyy', 'en-US' ) AS 'DateTime Result'
Output :
DateTime Result |
---|
23/11/2020 |
Example-4 : Formatted current time with AM or PM.
SELECT FORMAT(SYSDATETIME(), N'hh:mm tt');
Output :
10:11 AM
Example-5 : Example of CURRENCY format.
SELECT FORMAT(1, 'C', 'in-IN') AS 'INDIA', FORMAT(1, 'C', 'ch-CH') AS 'CHINA', FORMAT(1, 'C', 'sw-SW') AS 'SWITZERLAND', FORMAT(1, 'C', 'us-US') AS 'USA';
Output :
INDIA | CHINA | SWITZERLAND | USA |
---|---|---|---|
₹1.00 | CHF1.00 | ¤1.00 | $1.00 |