Google Answers Logo
View Question
 
Q: SQL Server query ( No Answer,   3 Comments )
Question  
Subject: SQL Server query
Category: Computers > Programming
Asked by: b3jz-ga
List Price: $20.00
Posted: 26 Jan 2006 12:26 PST
Expires: 25 Feb 2006 12:26 PST
Question ID: 438014
SQL statement (MS SQL Server) that retrieves all rows (using one
column) that do not consist of only a-z, A-Z or 0-9. SO would not
return a row that had 'sdfjnjd7733NNNND' but would return
'sdcsnjn-77N' for example
Answer  
There is no answer at this time.

Comments  
Subject: Re: SQL Server query
From: andyclap-ga on 26 Jan 2006 15:20 PST
 
Presuming you're working with normal ascii data, the easiest way to do
this is to check the field being "not like" any of the disallowed
characters.

Unfortunately the like expression doesn't allow ranges by character
code, but you can use the char() function to get a particular
character, and it seems to work OK for ranges.

For normal ascii data, the ranges to exclude are space to the
character before 0, the character after 9 to the character before A,
the character after Z to the character before a, then the character
after z to the end of the characters.

so...

select *
from MyTable
where MyField not like
'%[ -' + char(ascii('0')-1) 
+ char(ascii('9')+1) + '-' + char(ascii('A')-1) 
+ char(ascii('Z')+1) + '-' + char(127) + ']%'
Subject: Re: SQL Server query
From: mathtalk-ga on 30 Jan 2006 18:40 PST
 
I see this as being a simple 'like' query using wildcards and a not
(^) operator applied to a range.

Let's suppose that the table is myTable and has a varchar filed called myField.

The query I have in mind would be:

select * from myTable where myField like '%[^a-zA-Z0-9]%'

In other words the query requires that somewhere in column myField,
there's a character _other than_ an alphanumeric character.


regards, mathtalk-ga
Subject: Re: SQL Server query
From: digichaos-ga on 03 Feb 2006 10:09 PST
 
Maybe the best approach would be to look at it from the other way
around. Instead of looking at the a-z, A-Z, and 0-9 you should find
all items that contain the other characters you want.

Either way, here is a SQL function you can add to your database and an
example of how you can call it to return back data.

You can copy and paste this into SQL Query Analyzer and run it to put
this function into your database:

--- START PASTE

CREATE FUNCTION
dbo.find_regex
(
	@regex varchar(255),
	@fieldname varchar(8000)
)
RETURNS int
AS
BEGIN
	declare @obj int
	declare @res int
	declare @match bit
	set @match=0
 
	exec @res=sp_OACreate 'VBScript.RegExp',@obj OUT
	IF (@res <> 0)
	BEGIN
	      RETURN NULL
	END
	exec @res=sp_OASetProperty @obj, 'Pattern', @regex
	IF (@res <> 0)
	BEGIN
	      RETURN NULL
	END
	exec @res=sp_OASetProperty @obj, 'IgnoreCase', 1
	IF (@res <> 0)
	BEGIN
	      RETURN NULL
	END
	exec @res=sp_OAMethod @obj, 'Test',@match OUT, @fieldname
	IF (@res <> 0)
	BEGIN
	      RETURN NULL
	END
	exec @res=sp_OADestroy @obj
	return @match
END
GO

--- END PASTE


Then to use this query to get back all the rows that contain
~!@#$%^&*()_+-= in a field called "Name":

SELECT * FROM tbl_Site WHERE dbo.find_regex('\b[~!@#$%^&*()_+\-=]',Name) <> 0

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy