Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 query to find rows containing non-alphanumeric characters in a column

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 that did what I wanted. I have seen many examples which can tell you if it contains alphanumeric 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.

like image 581
Jay Avatar asked Dec 17 '09 06:12

Jay


People also ask

How do you find non alphanumeric characters?

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, a total of 62 characters, and we can use regex [a-zA-Z0-9]+ to matches alphanumeric characters.

How do I find the characters in a column in SQL?

LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.

How do you check if a string is alphanumeric in SQL?

Answer: To test a string for alphanumeric characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. The string value that you are testing. This function will return a null value if string1 is alphanumeric.

How do I find the alphanumeric values of a column in SQL Server?

Using LIKE clause Val LIKE '%[A-Z]%', it ensures that string should contain alphanumeric characters. Val LIKE '%[0-9]%', it ensures that string should contain numeric characters. Lets see the output of T-SQL, you can see it returns only alphanumeric string only.


1 Answers

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 
like image 164
beach Avatar answered Oct 04 '22 03:10

beach