views:

222

answers:

2

I was actually asked this myself a few weeks ago, whereas I know exactly how to do this with a SP or UDF but I was wondering if there was a quick and easy way of doing this without these methods, I'm assuming that there is and I just can't find it.

A point I need to make is that although we know what characters are allowed (a-z, A-Z, 0-9) we don't want to specify what is not allowed (#@!$ etc...). Also, we want to pull the rows which have the illegal characters so that it can be listed to the user to fix (as we have no control over the input process we can't do anything at that point).

I have looked through SO and google previously, but was unable to find anything which did what I wanted. I have seen many examples which can tell you if it contains alpha-numeric characters, or doesn't, but something that is able to pull out an apostrophe in a sentence I have not found in query form.

Please note also that values can be null or '' (empty) in this varchar column.

+2  A: 

Won't this do it?

SELECT * FROM TABLE
WHERE COLUMN_NAME LIKE '%[^a-zA-Z0-9]%'

Setup

use tempdb
create table mytable ( mycol varchar(40) NULL)

insert into mytable VALUES ('abcd')
insert into mytable VALUES ('ABCD')
insert into mytable VALUES ('1234')
insert into mytable VALUES ('efg%^&hji')
insert into mytable VALUES (NULL)
insert into mytable VALUES ('')
insert into mytable VALUES ('apostrophe '' in a sentence') 

SELECT * FROM mytable
WHERE mycol LIKE '%[^a-zA-Z0-9]%'

drop table mytable

Results

mycol
----------------------------------------
efg%^&hji
apostrophe ' in a sentence
beach
LIKE doesn't support regexes, only the wildcard. And we don't know what version currently..
OMG Ponies
We don't need a RegEx to solve this problem. Unless I am missing something.
beach
Let me rephrase - we don't need full RegEx support. Using the simple pattern matching that LIKE supports (or PATINDEX) should suffice. We only need to find a single character that is not in the allowed list.
beach
Could have sworn I did this at some point, I kind of assumed that because '' does not contain any alphanumeric characters that would be returned also. Thanks heaps, that's the answer :)
Jay
+2  A: 

Sql server has very limited Regex support. You can use PATINDEX with something like this

PATINDEX('%[a-zA-Z0-9]%',Col)

Have a look at PATINDEX (Transact-SQL)

and Pattern Matching in Search Conditions

astander
If you want to use PATINDEX, you need to do something like this:"SELECT * FROM mytable WHERE PATINDEX('%[^a-zA-Z0-9]%',mycol) > 1"
beach
Yes, this is correct. Selecting it in the column list will only show you where the values are.
astander