SQL STR Function

The SQL STR function is a SQL String Function used to return the string representation of the numeric expression. The syntax of the SQL Server STR Function is

SELECT STR (Float_Expression, Length, Decimal)
FROM [Source]

his String STR function will return VARCHAR. The list of arguments available for STR in SQL Server is:

  • Float_Expression: Please specify the valid Expression for which you want to convert as character data. Please specify any expression of a Float data type with decimal points.
  • length: This is an optional argument, and it represents the total string length. The value assigned to length argument includes a decimal point, sign, spaces, and digits. If you omit this argument, then SQL will assign default length as 10.
  • decimal: This is an optional argument, and it represents the total number of decimal places to display by the string result. The value assigned to the decimal argument must be less than or equal to 16, and if you assign more than 16, then the result will be truncated to 16.

SQL STR Function Example 1

The Sql Server STR Function is used to return the string representation of the float values. The following query will show you multiple ways to use this function.

-- SQL STR Function example
DECLARE @Num FLOAT
SET @Num = 786.387

-- No Length, Decimal arguments. @Num value will be rounded 
SELECT STR(@Num) AS Result1

-- Length = 7, No Decimal argument. 
-- Although length is 7, @Num value will be rounded because decimal places defaults value is 0
SELECT STR(@Num, 7) AS Result2
SELECT STR(@Num, 7, 0) AS Result3

-- Length = 7, Decimals = 1, 2 Resulting string will return 1 and 2 decimal values
SELECT STR(@Num, 7, 1) AS Result4
SELECT STR(@Num, 7, 2) AS Result5

SELECT STR(@Num, 6, 1) AS Result6
-- Length = 5, Decimals = 3.
SELECT STR(@Num, 5, 3) AS Result7
SQL STR Function 1

Within this SQL STR function example query, For both the statements, Length = 7, and for the first one No Decimal argument, for the second statement decimal = 0. Although the length value is 7, the @Num value rounded because decimal places default value is 0.

SELECT STR(@Num, 7) AS Result2
SELECT STR(@Num, 7, 0) AS Result3

Here, Length = 5, and Decimals = 3. Although the decimal value is 3, the @Num value is rounded to one because length value is 5 (and length of 786.4 is 5, including the decimal point)

SELECT STR(@Num, 5, 3) AS Result7

SQL STR Function Example 2

In this example, We are going to use the SQL STR function with uneven lengths and decimals values.

-- STR SQL example
SELECT STR(786.387) AS Result1
SELECT STR(786.387, 3) AS Result2

SELECT STR(786.387, 2) AS Result3
SELECT STR(786.387, 2, 2) AS Result4

SELECT STR(786.387, 9, 5) AS Result5
SELECT STR(FLOOR(786.387), 9, 4) AS Result6
SELECT STR(CEILING(786.387), 9, 4) AS Result7
SQL STR Function 2

Here, we are assigning the length value less than the left-hand side of the decimal point. The SQL Server will return the output as ** 

SELECT STR(786.387, 2) AS Result3
SELECT STR(786.387, 2, 2) AS Result4

Next, we are applying the SQL STR function on Floor and ceiling values. Though the values rounded to nearest values, this String Function is returning 4 zeros after the decimal point because we assigned the decimal argument to 4, and length = 9 (length of 787.0000 is 9, including the decimal point)

SELECT STR(FLOOR(786.387), 9, 4) AS Result6
SELECT STR(CEILING(786.387), 9, 4) AS Result7

STR Function Example 3

In this example, we are going to implement the SQL STR function on the numeric columns. For this STR demo, We are going to use the below-shown data

SQL STR Function 0

The following statement will return the string representation of [Yearly Income] column and [Sales] column.

-- SQL STR example
SELECT [FirstName] 
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,STR([YearlyIncome], 4, 2) AS [Str Income]
      ,[Sales]
      ,STR([Sales], 7, 3) AS [Str Sales]
  FROM [SQL Tutorial].[dbo].[Employee]

From the below screenshot you can observe that, str Income is returning **** because we assigned the length value less than the yearly income value.

SQL STR Function 3