SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP

Remove Non-Numeric Character in SQL String Expression

This T-SQL tutorial includes a user-defined SQL Server function code to clear alpha characters from an input string expression. Database programmers can use the given user-defined function in their development codes for removing characters except numbers from 0 to 9.

Here is a very basic user defined SQL function which removes unwanted alpha characters which does not represent numbers in a string expression
If database developers check the regular expression used in PatIndex SQL function, they will see that the regular expression continuously checking characters representing characters except 0 to 9.

create function ClearNonNumericCharacters(@str nvarchar(max))
returns nvarchar(max)
 while patindex('%[^0-9]%', @str) > 0
  set @str = stuff(@str, patindex('%[^0-9]%', @str), 1, '')
 return @str

remove non-numeric characters from string using SQL function

Here to use user defined SQL Server function in a sample case for Transact-SQL developers

select dbo.ClearNonNumericCharacters('19Kodyaz0 4*6/2=? QA=>9')

select phone, dbo.ClearNonNumericCharacters(phone) from PhoneNumbers

Here is the output of above sample SQL function

clearing non-numeric values in a string expression in SQL Server

This SQL function code can be used after changing the PatIndex regular expression part to remove numeric characters in SQL string expression.

SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.