首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-08-14 
怎样去掉小数点后多余的零比如12.20,s?q?l中有这样的函数吗?[解决办法]SQL codecreate function rtrim0(@v

怎样去掉小数点后多余的零
比如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) 

热点排行