STR (Transact-SQL)STR (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回從數值資料轉換而來的字元資料。Returns character data converted from numeric data.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

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

引數Arguments

float_expressionfloat_expression
這是含小數點的近似數值 (float) 資料類型的運算式。Is an expression of approximate numeric (float) data type with a decimal point.

lengthlength
這是總長度。Is the total length. 其中包括小數點、正負號、數字和空格。This includes decimal point, sign, digits, and spaces. 預設值是 10。The default is 10.

decimaldecimal
這是小數點右方的位數。Is the number of places to the right of the decimal point. decimal必須小於或等於 16。decimal must be less than or equal to 16. 如果 decimal 大於 16,則結果就會截斷為小數點右方的十六位數。If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.

傳回類型Return Types

varcharvarchar

RemarksRemarks

如果提供的話,STR 的 lengthdecimal 參數值應該是正數。If supplied, the values for length and decimal parameters to STR should be positive. 根據預設,如果 decimal 參數是 0,這個數字會捨入到整數。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). 在指定長度中,短的 float_expression 會靠右對齊,長的 float_expression 會在指定的小數位數截斷。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. 例如,STR(12 , 10) 所產生的結果是 12。For example, STR(12 , 10) yields the result of 12. 這會在結果集中靠右對齊。This is right-justified in the result set. 不過,STR(1223 , 2) 會將結果集截斷為 **。However, STR(1223 , 2) truncates the result set to **. 字串函數可以是巢狀函數。String functions can be nested.

注意

若要轉換成 Unicode 資料,請在 CONVERT 或 CAST 轉換函數內使用 STR。To convert to Unicode data, use STR inside a CONVERT or CAST conversion function.

範例Examples

下列範例會將五個位數和小數點組成的運算式轉換成六個位數的字元字串。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)  

即使數值資料巢狀放置在 STR 內,結果也是含指定格式的字元資料。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)  

另請參閱See Also

CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)
FORMAT (Transact-SQL)FORMAT (Transact-SQL)
字串函數 (Transact-SQL)String Functions (Transact-SQL)