Number of SQL Server classic functions go to 0

Source: Internet
Author: User

Demand:

For numeric information with a decimal point, remove the extra 0 after the decimal point

Possible situations:

1, the accuracy of the range, the presence of excess 0 eg:1234.3400 desired result is 1234.34

2, the accuracy of the larger appearance of the excess 0, or no actual fractional part of the data eg:1234.0 want the result of 1234

3, the end of the data is a decimal point, but there is no actual decimal eg:1234. The desired result is 1234

----Go to 0 function---Remove the extra 0 select dbo after the decimal point. Clearzero (' 1245.3400 ')---result is 1245.34Create function [dbo].[Clearzero](@Number varchar( $))returns varchar( $) asbegin    if @Number = "' or @Number  is NULL     ---if empty, return directly        return NULL    Else    begin        Declare @PointIndex int        Set @PointIndex = charindex('.',@Number)          if @PointIndex = 0 return @Number      ---If it is not a floating-point number, return directly        if @PointIndex = Len(@Number)return Replace(@Number,'.',"')----decimal points at the end of the number, directly delete the return        Else        begin            if  Right(@Number,1)= '0'               ---Recursive call, go to 0 operation            begin                Set @Number = substring(@Number,1,Len(@Number)- 1)                returnDbo. Clearzero (@Number)            End            Else                return @Number        End    End    return NULLEndGO

Number of SQL Server classic functions go to 0

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.