In SQL Server 2012 and later, there is the FORMAT() function which is responsible to format a value with the specified format such as format date/time values and number values.
This function accepts three arguments as given below syntax-
FORMAT(value, format, culture)
Parameter Description
value: Required. The value to be formatted
format: Required. The format pattern
culture: Optional. Specifies a culture (from SQL Server 2017)
examples -
- Country Specific Date Conversation,
- Custom Numeric Format- This consists of a series of custom numeric format specifiers that provide a pattern for your number.
- Format Number to Percentage - If you are trying to simply format a number as a percent with two decimal places
Example - The following example returns a simple date
formatted for different cultures.
DECLARE @d
DATETIME = '10/01/2019';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'USA'
,FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain'
,FORMAT ( @d, 'd', 'de-de' ) AS 'German'
,FORMAT ( @d, 'd', 'zh-cn' ) AS 'China';
SELECT FORMAT ( @d, 'D', 'en-US' ) AS 'US English Result'
,FORMAT ( @d, 'D', 'en-gb' ) AS 'Great Britain English Result'
,FORMAT ( @d, 'D', 'de-de' ) AS 'German Result'
,FORMAT ( @d, 'D', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';
|
Benefit
of this is you can control additional things like thousand separators and you
don't get that space between the number and '%'.
Remarks
- FORMAT returns NULL for errors other than a culture that is not valid. For example, NULL is returned if the value specified in format is not valid.
- The FORMAT function is nondeterministic.
- FORMAT relies on the presence of the .NET Framework Common Language Runtime (CLR).
- This function cannot be remoted since it depends on the presence of the CLR. Remoting a function that requires the CLR, could cause an error on the remote server.
- FORMAT relies upon CLR formatting rules, which dictate that colons and periods must be escaped. Therefore, when the format string (second parameter) contains a colon or period, the colon or period must be escaped with backslash when an input value (first parameter) is of the time data type.
To Learn more, please visit our blog at -
http://www.sql-datatools.com
To Learn more, please visit our YouTube channel at -
http://www.youtube.com/c/Sql-datatools
To Learn more, please visit our Instagram account at -
https://www.instagram.com/asp.mukesh/
To Learn more, please visit our twitter account at -
https://twitter.com/macxima
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command...
ReplyDeleteMySQL Online Training
MySQL Online Certification
MySQL Course Certification