StringToNumeric

ALTER FUNCTION [dbo].[ConvertStringToNumeric] (@Value text) RETURNS NUMERIC(12,2) AS
    BEGIN
   
            declare @string varchar(max)
            SET @string = CONVERT(VARCHAR(MAX), @Value)
            --set @string='abc93.3ad4'
            DECLARE @IncorrectCharLoc SMALLINT
            SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string)
            WHILE @IncorrectCharLoc > 0
            BEGIN
            SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')
            SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string)
            END
            SET @string = @string
            IF ISNUMERIC (@string)=1 AND LEN(@string)>0 AND @string != '.' AND @string != '-' AND @string != '.-'  AND Len(@string) <=8 AND @string != '-.'
            BEGIN
                Return CONVERT(NUMERIC(10,2),@string)
            END
            RETURN -1

    END

Comments

Popular posts from this blog

Querying a Data Table Using Select Method and Lambda Expressions in Dot Net

StringToTable

GetPhoneSSNFormat