当前位置: 代码迷 >> Sql Server >> float转换字符串有关问题
  详细解决方案

float转换字符串有关问题

热度:34   发布时间:2016-04-27 15:17:05.0
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 allselect 0.0852585 union allselect 0 union allselect 234.155544select *, case f1 when 0 then '' else convert(varchar,f1,128) end s1 from #/*f1                     s1---------------------- ------------------------------1.2345                 1.23450.0852585              0.08525850                      234.155544             234.155544*/
  相关解决方案