当前位置: 代码迷 >> Sql Server >> 怎么去掉小数点后多余的零
  详细解决方案

怎么去掉小数点后多余的零

热度:25   发布时间:2016-04-27 12:04:51.0
怎样去掉小数点后多余的零
比如12.20,s?q?l中有这样的函数吗?

------解决方案--------------------
SQL code
create function rtrim0(@val numeric(10,2))    returns varchar(20)asbegin  return left(@val,len(@val)-patindex('%[^0]%.%',reverse(@val))+1)endgoselect dbo.rtrim0(12.20)
------解决方案--------------------
修正一下,1楼没考虑小数点后面全是0的情况
SQL code
create function rtrim0(@val numeric(18,5))    returns varchar(20)asbegin  return     case       when @val=cast(@val as int)        then ltrim(cast(@val as int))      else        left(@val,len(@val)-patindex('%[^0]%.%',reverse(@val))+1)    endendgoselect dbo.rtrim0(12.20)
------解决方案--------------------
SQL code
declare @d decimal(10,2) set @d =  12.20select convert(float,@d)
  相关解决方案