Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server STR() Function - Get Number as String

In SQL Server, STR() function returns the numeric data as string.

STR(numeric_expression [,length  [ ,decimal]])

Parameters

numeric_expression: A numric expression with or without a decimal.

length: specifies the total length of the returned string, including decimal point, sign, digits, and spaces. It should be a positive integer. Default length is 10.

Note: The specified length should be greater than or equal to the length of the number before the decimal point plus the number sign if included.

decimal: is the number of places to the right of the decimal point. It must be less than or equal to 16. If the decimal is more than 16, then the result is truncated to 16 places to the right of the decimal point. It is a positive integer.

Return Value

Returns a numeric string of varchar type.

Get Numeric String from a Number

The following STR() function returns the numeric string.

Example: STR()
SELECT STR(123.76, 6, 2) AS Result

In the above example, STR(123.76, 6, 2) returns "123.76". 6 is the total length of the result including decimal point, 2 is decimal points in the result.

In the following example, STR(123.76, 6, 1) returns "123.8" because the decimal parameter is 1, so it will rounded up to largest value.

Example: STR()
SELECT STR(123.76, 6, 1) AS Result

The STR() function will return different result for different length parameter, as shown below.

Example: STR()
SELECT STR(123.76, 5, 2) AS Result1,
	STR(123.76, 4, 2) AS Result2

The following STR() function used with different length and decimal parameters:

Example:
SELECT STR(123.76, 6, 1) AS Result1,
	STR(123.76, 6, 2) AS Result2,
	STR(123.76, 7, 3) AS Result3,
	STR(123.76, 8, 4) AS Result4

If the length of the number exceeds the specified length, then it returns '**'.

Example:
SELECT STR(123.76, 2, 1) AS Result

If the specified length is bigger than the length of the given number and the decimal is equal to the decimal of the given number, then the number is returned unchanged.

Example:
SELECT STR(123.76, 8, 2) AS Result
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.