Share several commonly used and useful numeric processing custom functions in SQL Server

Source: Internet
Author: User

--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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.