STR() 把数值型数据转换为字符型数据,
格式为
STR (变量,length,decimal),分别代表变量,指定的变量返回的长度,指定返回的小数位数。如果没有指定长度,缺省的length 值为10, decimal 缺省值为0。
当length 或者decimal 为负值时,返回NULL;
declare @test1 float,@test2 varchar(20)
select @test1 =-20.233333
select @test2 ='hello,xixi'
select str(@test1,10,-20) + @test2
当length 小于小数点左边(包括符号位)的位数时,返回length 个
先服从length ,再取decimal ;
当返回的字符串位数小于length ,左边补足空
declare @test1 float,@test2 varchar(20)
select @test1 =20.23
select @test2 ='hello,xixi'
select str(@test1,10,2) + @test2
select len(str(@test1,10,2) + @test2 )
```![这里写图片描述](https://img-blog.csdn.net/20180428134306113?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZGVyc2VudmFl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
我们可以看出前面是有空格的,那是因为返回的字符串位数小于length ,左边补足空。我们可以进行如下的设置,
<div class="se-preview-section-delimiter"></div>
declare @test1 float,@test2 varchar(20)
select @test1 =20.233333333333333333333
select @test2 =’hello,xixi’
select str(@test1,10,10) + @test2
“`
declare @test1 float,@test2 varchar(20)
select @test1 =20.233333333333333333333
select @test2 ='hello,xixi'
select str(@test1,10,10) + @test2
当返回的字符串位数不小于length,就不会有空格
在实际运用中,当有空格时,一般会使用如下语句
select ltrim(str(@test1,20) + @test2 )