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

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse

返回由数字数据转换来的字符数据。Returns character data converted from numeric data. 字符数据右对齐,具有指定长度和十进制精度。The character data is right-justified, with a specified length and decimal precision.

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

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

备注

若要查看 SQL Server 2014 及更早版本的 Transact-SQL 语法,请参阅早期版本文档To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

参数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,则将结果截断为小数点右边的 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

备注Remarks

如果提供,则 STR 的 length 和 decimal 参数值应该是正数。If supplied, the values for length and decimal parameters to STR should be positive. 在默认情况下或小数参数为 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)
格式 (Transact-SQL)FORMAT (Transact-SQL)
字符串函数 (Transact-SQL)String Functions (Transact-SQL)