April 12, 2010 at 11:50 am
Hey all,
I am converting data from text-based reports, and reading the data into a SQL database. I am in charge of checking to ensure that the data was read-in correctly. One thing I want to check for is non-numeric characters (i.e. letters, punctuation) in a field that is a string, but should be filled with only numbers (e.g. zip code).
Does anyone know a concise way to bring up values that contain non-numeric characters? The only way that I know how to involves writing a long select query using 'like' conditions to specify the different characters I want to check for. I was hoping that maybe I could use some sort of value keyword to attempt to convert the zip codes to integers, and that it would let me know of any zip codes that contain non-numbers. I would appreciate any advice that anyone can give.
Thanks.
April 12, 2010 at 11:57 am
this is probably the easiest way: using the LIKE statement and the pseudo regular expression:
select zipcode,* from YourTable where zipcode like '%[^0-9]%'
that will find anything with characters outside of the 0-9 range;
in my case, i found a zipcode with the value "74346+"
Lowell
April 12, 2010 at 11:59 am
SELECT cols
FROM YourTable
where YourColumn like '%[0-9]%'
will return only rows with numbers.
SELECT cols
FROM YourTable
where YourColumn like '%[^0-9]%'
will return only rows at least one non-numeric value edit: character.
April 12, 2010 at 12:28 pm
lmu92 (4/12/2010)
SELECT cols
FROM YourTable
where YourColumn like '%[0-9]%'
will return only rows with numbers.
SELECT cols
FROM YourTable
where YourColumn like '%[^0-9]%'
will return only rows at least one non-numeric
valueedit: character.
Careful... the first one will return [font="Arial Black"]ANY [/font]row containing even just one digit regardless of what the other characters are. The second one is correct.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 12, 2010 at 2:19 pm
That expression looks handy. Thanks!
April 12, 2010 at 3:16 pm
One more question on the topic. I've been trying to do some reading up on regular expressions, but most of the resources I get seem to be concerned with implementing them and are quite involved.
Is there a list of expressions like "[^0-9]" that it might be worthwhile to take a look at? I'd be interested in familiarizing myself with other shorthands expressions.
Thanks.
April 12, 2010 at 5:49 pm
Very handy... thanks
April 13, 2010 at 5:32 pm
cjohn5552 (4/12/2010)
One more question on the topic. I've been trying to do some reading up on regular expressions, but most of the resources I get seem to be concerned with implementing them and are quite involved.Is there a list of expressions like "[^0-9]" that it might be worthwhile to take a look at? I'd be interested in familiarizing myself with other shorthands expressions.
Thanks.
I'm sure there is... I just don't know where because I've not needed full blown Regex in T-SQL yet.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 19, 2010 at 12:08 pm
Hey,
I think I found a useful resource for the info I was looking for. It's in Books Online. The document is called "How to: Search with Regular Expressions". Hope it helps.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply