We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

case sensitivity in Transact-SQL statement

tdeinzer2005
tdeinzer2005 asked
on
Medium Priority
933 Views
Last Modified: 2008-12-24
Is there a way to make a where clause in a query (executed from Query Analyzer) case sensitive.  For instance, if you wanted to find records based on how people entered a country name:

where country = 'usa'
where country = 'Usa'
where country = 'USA'

Normally, of course, these there queries would produce the same results.

Comment
Watch Question

There is a server setting that can make the server case sensative.  But not an idividual query, I don't think.
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
I think if you cast both to varbinary you can get a case-sensitive match, although that prevents use of an index on that column (if you need a way around that, please let me know):

where cast(country as varbinary(100)) = cast('usa' as varbinary(100))



Commented:
you should use case sensitive collation .

lets see... try this

select * from tblmember
where address1  COLLATE SQL_Latin1_General_CP1_CS_AS = 'usa'

by default collations are case insensitive such as SQL_Latin1_General_CP1_CI_AS (notice CI before AS which means case insensitive)... all you need to do is check the collation you have on the field and then apply similar one with CS in it.


few common ones are SQL_Latin1_General_CP1_CS_AS, Latin1_General_CP1_CS_AS, latin1_general_cs_as

Cheers
Commented:
Like what randeeps has mentioned before, default settings on SQL Server is case insensitive.

But don't worry too much as in SQL Server 2000 you can change the collation of the column using ALTER statement. for example,

ALTER TABLE dbo.Customers ALTER COLUMN CustID char(8) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL
GO

The above will change the column to case sensitive, then once you start storing data it will be case sensitive. So you original queries,

where country = 'usa'
where country = 'Usa'
where country = 'USA'

Will yields different result.

If after you changed to case sensitive, and you want to search by case insensitive, just do an upper() or lower() when comparing.

Refer to the following link, very USEFUL. Trust me.

http://vyaskn.tripod.com/case_sensitive_search_in_sql_server.htm

Regards,

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.