日期:2014-05-18  浏览次数:20652 次

float转换字符串问题
数据库表 T 列名 f1(float) 存有小数
f1
---------
1.2345
0.0852585
0
234.155544

需实现功能查询输出数据,非0的输出原始值 , 为0输出空

通过一个存储过程实现:
  create table #temptable(
  [c1] nvarchar(100)
  ) 

  insert into #temptable(c1)
  select case f1 when 0 then '' else case(f1 as nvarchar) end as c1 from T
  select * from #temptable
  drop table #temptable

----------------------------
结果:
c1
-----
1.2345
0.0852585

234.156
-----------------------------
问题是,第一个和第二个小数正常,第三个0也变为了空,但第四个数被四舍五入了,实在没搞明白????

------解决方案--------------------
SQL code
-- 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(f1 float)
insert into #
select 1.2345 union all
select 0.0852585 union all
select 0 union all
select 234.155544

select *, case f1 when 0 then '' else convert(varchar,f1,128) end s1 from #

/*
f1                     s1
---------------------- ------------------------------
1.2345                 1.2345
0.0852585              0.0852585
0                      
234.155544             234.155544
*/