Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

SQL STR Function

by suresh

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]

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.

Return Type

This String STR function will return VARCHAR

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

OUTPUT

SQL STR Function 1

ANALYSIS

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

OUTPUT

SQL STR Function 2

ANALYSIS

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]

OUTPUT

SQL STR Function 3

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

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy