Script to calculate Precision & Scale of a Decimal Value

  • Comments posted to this topic are about the item Script to calculate Precision & Scale of a Decimal Value

    --In 'thoughts'...
    Lonely Rogue

  • DECLARE @D DECIMAL(18,9)

    ,@S VARCHAR(20)

    ,@R VARCHAR(20)

    ,@Pos SMALLINT

    SET @D = 5.1235698

    SET @S = CAST(@D AS VARCHAR(20))

    SET @r = REVERSE(SUBSTRING(@S, CHARINDEX('.', @S) + 1, 20))

    SET @Pos = PATINDEX('%[1-9]%' , @r)

    IF @Pos = 0

    SELECT 0

    ELSE

    SELECT LEN(SUBSTRING(@R, @Pos, 20)) AS Length

  • That is true.. your script evaluates in the same way as mine does. As noted, there exists many approaches that includes yours.

    However, the SUBSTRING function along with CHARINDEX seems costlier than the PARSENAME...

    Neverthless, thanks for adding this code piece 🙂

    --In 'thoughts'...
    Lonely Rogue

  • Thanks for the script.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply