tags:

views:

500

answers:

2

I have a SQL statement that looks like:

SELECT [Phone]
FROM [Table]
WHERE
(
    [Phone] LIKE '[A-Z][a-z]'
    OR [Phone] = 'N/A'
    OR [Phone] LIKE '[0]'
)

The part I'm having trouble with is the where statement with the "LIKEs". I've seen SQL statements where authors used likes statements in the way I'm using them above. At first, I thought this might be a version of Regular Expressions but I've since learned.

Is anyone familiar with using like statements in such a way. Note: the "N/A" is working fine.

What I need to match is phone numbers that have characters. Or phone numbers which contain nothing but zero.

Thank you much,
Frank

+4  A: 

Check here.

[] matches a range of characters.

I think you want something like this:

SELECT [Phone]
FROM [Table]
WHERE
(
    [Phone] LIKE '%[A-Z]%'
    OR [Phone] LIKE '%[a-z]%'
    OR [Phone] = 'N/A'
    OR [Phone] LIKE '0'
)
Forgotten Semicolon
wow, didn't know that was possible
Danimal
Forgotten Semicolon (awesome user name), thank you. That link is exactly what I needed. I couldn't find it on a google search. Your example also helped realize my mistake.
Frank V
+2  A: 

Try using the t-sql ISNUMERIC function. That will show you which ones are/are not numeric.

You may also need to TRIM or REPLACE spaces to get what you want.

For example, to find valid phone numbers, replace spaces with '', test with ISNUMERIC, and test with LEN.

Although I will warn you, this will be tedious if you have to deal with international phone numbers.

The thing to note with your SQL above, is that SQL Server doesn't understand Regex.

therealhoff
Thank you for your response. I thought of using the IsNumeric function but if I'm not mistaken, that function is buggy. I do have to deal with international phone numbers and that is why I don't think this solution is for me. Thank you much!
Frank V