Saturday 24 September 2016

SQL function to remove non numeric characters

SQL function to remove non numeric characters
Description:

In this example we explain that how to remove non numeric character from string in SQL Server.or fetch the string without non alphabet character or removing non numeric character from string using SQL Server function.

Here we create one SQL Server user defined function that will return string or text with removing non numeric character in SQL Server.here we simply input the value to SQL Server function and it will returns the string or value with removing non numeric character.

So below is the SQL Server user defined function that will remove non numeric characters from the given input value.
Function:

CREATE Function [dbo].[RemoveNonNumaricCharacters](@Val VarChar(1000))
Returns VarChar(1000)
AS
Begin

    Declare @RegExp as varchar(50)
    Set @RegExp = '%[^0-9]%'
    While PatIndex(@RegExp, @Val) > 0
        Set @Val = Stuff(@Val, PatIndex(@RegExp, @Val), 1, '')   
    Return @Val
End
--Execute Query After Create above Function

SELECT dbo.RemoveNonNumaricCharacters('Kirit How r U ? 123')
This entry was posted in :

2 comments: