将big float转换为varchar SQL Server

时间:2011-03-01 00:18:23

标签: sql-server-2008

当我转换一些像我们这样的大花车时:

61326085092
161569087548
169193968080
208082024640
230160816344

使用后

Cast(@FloatValue as nvarchar)

我明白了:

61326100000
161569000000
169194000000
208082000000
230161000000

这真的很糟糕......所以我怎样才能获得准确的值?

我正在做的查询是:

INSERT INTO #someT (id,Value) values('''+Cast(@counterI+1 as nvarchar)+''','''+Cast(@Val as nvarchar)+''')';

4 个答案:

答案 0 :(得分:3)

对于您发布的号码LTRIM(STR(f,32))运行正常。

with floats(f) as
(
SELECT cast(61326085092 as float) union all
SELECT 161569087548 union all
SELECT 169193968080 union all
SELECT 208082024640 union all
SELECT 230160816344  union all
SELECT 230160816344.12
)
select f, CAST(f as nvarchar) as v, LTRIM(STR(f,32)) as s1, LTRIM(STR(f,32,10)) as s2
FROM floats

返回

f                      v                  s1             s2
---------------------- ----------------- -------------- --------------------------------
61326085092            6.13261e+010        61326085092           61326085092.0000000000
161569087548           1.61569e+011       161569087548          161569087548.0000000000
169193968080           1.69194e+011       169193968080          169193968080.0000000000
208082024640           2.08082e+011       208082024640          208082024640.0000000000
230160816344           2.30161e+011       230160816344          230160816344.0000000000
230160816344.12        2.30161e+011       230160816344          230160816344.1200000000

答案 1 :(得分:1)

这是因为精确。

尝试使用Float(53)Decimal(38,18)

你真的使用花车吗?您发布的数字看起来像整数......

答案 2 :(得分:1)

Cast(cast(@FloatValue as numeric) as nvarchar)

为您的案例选择合适的numeric比例和精度。你总是会遇到浮点数和实数的这个问题,它们本质上是不精确的。

答案 3 :(得分:0)

这些是否总是整数值(意味着没有小数点),如果是这样,则转换为bigint而不是

declare @f float =230160816344

select convert(bigint,@f)