--several commonly used custom SQL functions for digital processing--decimal starts with no 0 and end superfluous 0 handlesCREATE function [dbo].[formatfloat](@dec decimal( -,Ten)) returns varchar( -) as begin Declare @inValue varchar( -); Set @inValue =(CONVERT(decimal( -,Ten),@dec)); Declare @returnValue varchar( -) if(@inValue="') Set @returnValue="' --Empty time is empty.Else if(charindex('.',@inValue)='0') Set @returnValue=@inValue --for non-decimalElse if(substring(Reverse(@inValue),Patindex('%[^0]%',Reverse(@inValue)),1)='.') Set @returnValue =left(@inValue,Len(@inValue)-Patindex('%[^0]%',Reverse(@inValue)))--for all 0 after the decimal point Else Set @returnValue =left(@inValue,Len(@inValue)- Patindex('%[^0]%.%',Reverse(@inValue))+1)--any other situationreturn @returnValue--decimal format to thousands of bits formatCREATE function [dbo].[FORMATFLOAT3](@dec decimal( -,Ten)) returns varchar( -) as beginreturn CONVERT(VARCHAR( -),cast(@dec as Money),1);End--decimal format to the amount of two digits after the decimal point formatCREATE function [dbo].[FORMATFLOAT2](@dec decimal( -,Ten)) returns varchar( -) as beginreturn CONVERT(VARCHAR( -),CAST(@dec as decimal( -,2))) ;End
--Execution effect
SELECT dbo. Formatfloat (1233456.12800)
1233456.128
SELECT dbo. FORMATFLOAT3 (1233456.12800)
1,233,456.13
SELECT dbo. FORMATFLOAT2 (1233456.12800)
1233456.13