SQL Server String Functions
SQL Server string functions are scalar functions that perform an operation on a string input value and return a string or numeric value.
STR Function
The STR function converts a numeric value into a string. This function can be considered as a special case of CAST or CONVERT functions, both of which let you convert the variable from one data type into another compatible datatype. The STR function allows specifying the length of the string variable returned, as well as how many decimal points to include in the output. The syntax is:
STR(numeric value, length, decimal)
The length argument specifies the total length of the string. For example, '2.4503' consists of 6 characters. The decimal argument specifies how many characters are allowed to the right of the decimal point. For example, the following query returns a rounded currency rate as a string; the string has a total of six characters, five of which could be to the right of the decimal point:
SELECT EndOfDayRate, STR(EndOfDayRate, 6, 4) AS string_value
FROM factCurrencyRate WHERE CurrencyKey = 3 AND timeKey = 2
Results:
EndOfDayRate string_value
1.000900811 1.0009
Tags:
SQL Server string functions are scalar functions that perform an operation on a string input value and return a string or numeric value.
STR Function
The STR function converts a numeric value into a string. This function can be considered as a special case of CAST or CONVERT functions, both of which let you convert the variable from one data type into another compatible datatype. The STR function allows specifying the length of the string variable returned, as well as how many decimal points to include in the output. The syntax is:
STR(numeric value, length, decimal)
The length argument specifies the total length of the string. For example, '2.4503' consists of 6 characters. The decimal argument specifies how many characters are allowed to the right of the decimal point. For example, the following query returns a rounded currency rate as a string; the string has a total of six characters, five of which could be to the right of the decimal point:
SELECT EndOfDayRate, STR(EndOfDayRate, 6, 4) AS string_value
FROM factCurrencyRate WHERE CurrencyKey = 3 AND timeKey = 2
Results:
EndOfDayRate string_value
1.000900811 1.0009