SQL 数据库查询出来的数据集调用储存过程删除右边多余的0

CREATE FUNCTION dbo.trim0(@val numeric(18,7))
    RETURNS VARCHAR(20)
AS
BEGIN
  RETURN   
  CASE 
    WHEN CAST(@val AS INT)=@val THEN LTRIM(CAST(@val AS INT)) 
    ELSE LEFT(@val,LEN(@val)-PATINDEX('%[^0]%.%',REVERSE(@val))+1)
  END
END
GO

drop table test

select dbo.trim0(col1) as col1,dbo.trim0(col2) as col2,dbo.trim0(col3) as col3
from test

/**
col1                 col2                 col3
-------------------- -------------------- --------------------
1.224                5.66                 90.31
2.724                4.96                 94.34
2.724                4.96                 94.34
5.524                3.64                 7.32054
5.524                3.64                 7.32054
5.524                3.64                 7.32054
5.524                3.64                 7.32054
6.324                8.06                 2.3105
6.324                8.06                 2.3105
6.324                8.06                 2.3105

你可能感兴趣的:(sql,数据库,function,table)