case sensitivity in Transact-SQL statement

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.

tdeinzer2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adwisemanCommented:
There is a server setting that can make the server case sensative.  But not an idividual query, I don't think.
0
Scott PletcherSenior DBACommented:
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))



0
randeepsCommented:
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
0
J2811Commented:
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,
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.