Click to Rate and Give Feedback
MSDN
MSDN Library
SQL Server
SQL Server 2008
Database Engine
Technical Reference
 STR (Transact-SQL)
Community Content
In this section
Statistics Annotations (2)
Collapse All/Expand All Collapse All
Other versions are also available for the following:
SQL Server 2008 Books Online (October 2009)
STR (Transact-SQL)

Returns character data converted from numeric data.

Topic link icon Transact-SQL Syntax Conventions

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

Is an expression of approximate numeric (float) data type with a decimal point.

length

Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.

decimal

Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.

char

If supplied, the values for length and decimal parameters to STR should be positive. The number is rounded to an integer by default or if the decimal parameter is 0. The specified length should be greater than or equal to the part of the number before the decimal point plus the number's sign (if any). A short float_expression is right-justified in the specified length, and a long float_expression is truncated to the specified number of decimal places. For example, STR(12,10) yields the result of 12. This is right-justified in the result set. However, STR(1223,2) truncates the result set to **. String functions can be nested.

ms189527.note(en-us,SQL.100).gifNote:
To convert to Unicode data, use STR inside a CONVERT or CAST conversion function.

The following example converts an expression that is made up of five digits and a decimal point to a six-position character string. The fractional part of the number is rounded to one decimal place.

SELECT STR(123.45, 6, 1);
GO

Here is the result set.

------
 123.5

(1 row(s) affected)

When the expression exceeds the specified length, the string returns ** for the specified length.

SELECT STR(123.45, 2, 2);
GO

Here is the result set.

--
**

(1 row(s) affected)

Even when numeric data is nested within STR, the result is character data with the specified format.

SELECT STR (FLOOR (123.45), 8, 3;)
GO

Here is the result set.

--------
 123.000

(1 row(s) affected)
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
More Complex Formatting      Rare Breed   |   Edit   |   Show History
select
name = '"' +name+ '"'" ,

crDate = ' " ' + convert(varchar(8), crDate, 112) + ' " '

from master..sysobjects
Tags What's this?: Add a tag
Flag as ContentBug
Error in sample code      Jeff Heiner   |   Edit   |   Show History
The sample code has an error. The semicolon is not at the end of the statement.

SELECT STR (FLOOR (123.45), 8, 3;)
GO

Corrected code:
SELECT STR (FLOOR (123.45), 8, 3);
GO
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker