how to detect mixed case (SQL can be so insensitive)

August 11th, 2010 § Leave a Comment

So I was trying to find cities in an address table with mixed case names (like, “Seattle” instead of “SEATTLE”, etc). I was completely stumped until my search turned up the Collate clause. Which I think I used once last year, and then forgot about.

select * from tableX
where fieldY not like upper(fieldY) COLLATE SQL_Latin1_General_CP1_CS_AS
and fieldY not like lower(fieldY) COLLATE SQL_Latin1_General_CP1_CS_AS

The logic could also be switched around to detect all upper case or all lower case conditions. Getting a little fancy with substring, you could also look for terms that begin with a capital letter or some other pattern.

Note: COLLATE has to come after each condition that it applies to.

Advertisement

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading how to detect mixed case (SQL can be so insensitive) at gghaley.

meta

Follow

Get every new post delivered to your Inbox.