Function to Clear Non-Numeric Characters in SQL Server

Hello everyone,

In this article, I will give information about the function that clears non-numeric characters in SQL Server.

In SQL Server, in some cases, we may want to remove parentheses, dashes, etc. on a numeric text.

You can easily do this with the help of the function below.

 

CREATE FUNCTION RemovingNonNumericCharacters
(
    @Contents NVARCHAR(512)
)
RETURNS NVARCHAR(512)
AS
BEGIN
    DECLARE @Cleanedcontent NVARCHAR(512);
    SELECT @Cleanedcontent = @Contents;
    WHILE PATINDEX('%[^0-9]%', @Cleanedcontent) > 0
    SELECT @Cleanedcontent
        = REPLACE(@Cleanedcontent, SUBSTRING(@Cleanedcontent, PATINDEX('%[^0-9]%', @Cleanedcontent), 1), '');
    RETURN @Cleanedcontent;
END;

--Use of Function

SELECT dbo.RemovingNonNumericCharacters('0(544)-573-25-84');

When you run the code, you will get a result like the one below.

Function to Clear Non Numeric Characters in SQL Server

As you can see, non-numeric characters have been cleared.

Good luck to everyone in business and life.

306 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!