Click here to Skip to main content
15,885,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts

Some users had inserts something other than numbers in the 'Tel' column.
Now I want to remove them.

There is a query to find these kind of records:
SQL
Select    Tel
          From    Person
          Where   Tel	Like	N'%[أ-ي]%'  And
                  Tel   Is Not  Null

Results:
    Tel
--------------
داخلي 298
داخلي 111
مغازه 2701646
2802301 منزل

How I can Remove the extra info in a sql query?
Posted
Comments
Amir Mahfoozi 25-Nov-13 7:01am    
Replace "Select Tel" with "Delete" and all of them will go.
Meysam Toluie 25-Nov-13 7:10am    
You didn't get my question.
See the accepted answer.
Thank you anyway.
Amir Mahfoozi 25-Nov-13 7:33am    
Ok I got it.

1 solution

Hi,

Try below sample TSQL to remove non numeric characters from any string/varchar.
To use in sql query you need to create function and then you can use in any sql query also.

SQL
DECLARE @cString    VARCHAR(32)
DECLARE @nPos    INTEGER
SELECT  @cString = '90$%45623 *6%}~:@'
SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)

WHILE @nPos > 0
BEGIN
SELECT @cString = STUFF(@cString, @nPos, 1, '')
SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)
END

SELECT @cString
 
Share this answer
 
Comments
Meysam Toluie 25-Nov-13 8:21am    
Thank you for your answer. What if I want to remove only the alphabets?
Dusara Maulik 25-Nov-13 9:08am    
use PATINDEX('%[a-z]%', @cString) instead of PATINDEX('%[^0-9]%', @cString)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900