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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With