views:

22

answers:

3

How can I copy the value of a field, but only its numbers?

I am creating a computed column for fulltext search, and I want to copy the values from my Phone Number fields (which are varchar) into it, but not with their formatting - numbers only. What is the command that would do this in my computed column formula?

Thank you!

A: 

Assuming there's only a couple of non-number characters, a nested replace functions do the trick:

select replace(replace(replace(col1,'-',''),'(',''),')','')
from YourTable

You can check if you caught all characters like:

select col1
from YourTable
where col1 not like '%[-()0-9]%'

(This example is checking for -, (), and numbers.)

Andomar
Is there a more generic way? I don't really want to restrict people from entering whatever they want the phone number to look like. It may include + (as in +1 800) or x (as in 1235551234 x 1234) and other characters.
Alex
+1  A: 

You are going to have to write a user defined function to do this. There are several ways to do this, here is one that I found with some quick Googling.

CREATE FUNCTION dbo.RemoveChars(@Input varchar(1000))
RETURNS VARCHAR(1000)
BEGIN
  DECLARE @pos INT
  SET @Pos = PATINDEX('%[^0-9]%',@Input)
  WHILE @Pos > 0
   BEGIN
    SET @Input = STUFF(@Input,@pos,1,'')
    SET @Pos = PATINDEX('%[^0-9]%',@Input)
   END
  RETURN @Input
END

Warning: I wouldn't put this in a WHERE condition on a large table, or in a SELECT that returns millions of rows, but it will work.

Ultimately you are probably better stripping the non-numeric characters out in the UI of your app than in DB code.

JohnFx
I need to strip out the other characters so that I can effectively search for phone numbers using full text search combinations, e.g. John 555* should be able yield John Smith (555) 123-4567. I'm creating a computed column which concatenates combinatory search terms (such as name and phone number). In that column the number needs to be free of any formatting. I'll probably end up storing the number twice... once without formatting and once as the user entered it. Then I can just copy the unformatted number into the computed column.
Alex
If you are putting this in a persisted computed column, you should only take the hit on updates/inserts, so as long as you don't do those in large chunks, it seems workable. Although I must admit this all seems pretty hacky. I'd still go with forcing a particular input format when the data goes into the system instead of allowing freeform entry.
JohnFx
Okay I'm using this now, and it works well. I'm against forcing a particular input format for phone numbers because international numbers can be quite strange, plus extensions etc. etc.
Alex
A: 

I'd create a user-defined function that you could use in your select and where criteria, maybe something like this:

DECLARE @position int, @result varchar(50)
SET @position = 1
SET @result = ''

WHILE @position <= DATALENGTH(@input)
    BEGIN
    IF ASCII(SUBSTRING(@input, @position, 1)) BETWEEN 48 AND 57
        BEGIN
        SET @result = @result + SUBSTRING(@input, @position, 1)
        END
    SET @position = @position + 1
    END

RETURN @result

Best of luck!

Funka
How would I call this function inside the computed column formula?
Alex