none
Minor issue with code formatter RRS feed

  • General discussion

  • If you try to format this block as SQL:

    CREATE FUNCTION dbo.StringToDecimal(@InputString varchar(20))

    RETURNS decimal(15,5)

    AS

    BEGIN

          DECLARE @WholeNumber decimal(15,5), @Numerator int, @Denominator int

     

          --SET Numerator to 0, and Denominator to 1, so that Numerator/Denominator will return 0

          --if we don't re-set their value

          SET @Numerator = 0

          SET   @Denominator = 1

     

     

     

          IF CHARINDEX(' ', @InputString) >--There is a space.  As such, there must be a fractional portion

          BEGIN

                DECLARE @Fractional varchar(20)

                SET         @Fractional = RIGHT(@InputString, LEN(@InputString) - CHARINDEX(' ', @InputString))

     

                SET @Numerator = LEFT(@Fractional, CHARINDEX('/', @Fractional) - 1)

                SET @Denominator = RIGHT(@Fractional, LEN(@Fractional) - CHARINDEX('/', @Fractional))

                SET @WholeNumber = LEFT(@InputString, CHARINDEX(' ', @InputString) - 1)

          END

         

          ELSE  --There is no fractional portion

          BEGIN

                SET @WholeNumber = @InputString

          END

     

          DECLARE @ReturnValue decimal(15,5)

     

          SET @ReturnValue = @WholeNumber + (@Numerator * 1.00000 / @Denominator)

          RETURN @ReturnValue

    END

     

     

    SELECT dbo.StringToDecimal('5')

    SELECT dbo.StringToDecimal('5 3/4')

    SELECT dbo.StringToDecimal('5 23/714')



    You get this:

    CREATE FUNCTION dbo.StringToDecimal(@InputString varchar(20))  
    RETURNS decimal(15,5)  
    AS 
    BEGIN 
        DECLARE @WholeNumber decimal(15,5), @Numerator int, @Denominator int 
     
        --SET Numerator to 0, and Denominator to 1, so that Numerator/Denominator will return 0  
        --if we don't re-set their value  
        SET @Numerator = 0  
        SET @Denominator = 1  
     
     
     
        IF CHARINDEX(' ', @InputString) > 0  --There is a space.  As such, there must be a fractional portion
        BEGIN
            DECLARE @Fractional varchar(20)
            SET     @Fractional = RIGHT(@InputString, LEN(@InputString) - CHARINDEX(' ', @InputString))
            SET @Numerator = LEFT(@Fractional, CHARINDEX('/', @Fractional) - 1)
            SET @Denominator = RIGHT(@Fractional, LEN(@Fractional) - CHARINDEX('/', @Fractional))
            SET @WholeNumber = LEFT(@InputString, CHARINDEX(' ', @InputString) - 1)
        END
        
        ELSE  --There is no fractional portion
        BEGIN
            SET @WholeNumber = @InputString
        END
        DECLARE @ReturnValue decimal(15,5)
        SET @ReturnValue = @WholeNumber + (@Numerator * 1.00000 / @Denominator)
        RETURN @ReturnValue
    END
    SELECT dbo.StringToDecimal('5')
    SELECT dbo.StringToDecimal('5 3/4')
    SELECT dbo.StringToDecimal('5 23/714')  
     
     
    It's obviously not picking up the closing quote on the CHARINDEX line.  I'm pretty sure that this is a Telerik issue, but I'll leave that to you to decide.

    Thanks,

    Aaron Alton
    Aaron Alton | thehobt.blogpot.com
    Saturday, December 13, 2008 5:04 AM