【sql server 函数】str()

函数定义:str(float_expression [ , length [ , decimal ] ])

函数功能:用于将数值数据转换为字符数据

参数说明:

  float_expression是一个可带有小数点的数字数据类型的表达式【必填】

  length表示转换为字符数据的总长度,它包括小数点、符号、数字以及空格,默认长度值为10【可无】

  decimal指定小数点后的位数,decimal必须小于或等于16,如果大于16,则会截断结果,使其保持小数点后有16位【可无】

转换规则:先看整数部分是否满足转换长度,若长度值小于整数长度返回“*”,若长度值大于整数长度,再看小数部分。小数部分能按要求转换后仍不足转换长度,再在左侧补空格

测试用例:

  1.只有一个参数

select replace( str(123),' ','0');  --0000000123
select replace( str(123.0),' ','0');--0000000123
select len(str(123));--10
select len(str(123.0));--10
View Code

  2.有两个参数

select replace( str(123,7),' ','0');  --0000123
select replace( str(123.0,7),' ','0');--0000123
select len(str(123,7));--7
select len(str(123.0,7));--7
View Code

  3.有三个参数

select '''' + str(123,1,2) + '''';--'*'
select '''' + str(123,2,2) + '''';--'**'
select '''' + str(123,3,2) + '''';--'123'
select '''' + str(123,4,2) + '''';--' 123'
select '''' + str(123,5,2) + '''';--'123.0'
select '''' + str(123,6,2) + '''';--'123.00'
select '''' + str(123.0,1,2) + '''';--'*'
select '''' + str(123.0,2,2) + '''';--'**'
select '''' + str(123.0,3,2) + '''';--'123'
select '''' + str(123.0,4,2) + '''';--' 123'
select '''' + str(123.0,5,2) + '''';--'123.0'
select '''' + str(123.0,6,2) + '''';--'123.00'
select '''' + str(123.0,7,2) + '''';--' 123.00'
View Code

 

posted @ 2020-04-16 16:43  单纯的桃子  阅读(2059)  评论(0编辑  收藏  举报