Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

case sensitivity in Transact-SQL statement

Posted on 2006-03-29
6
Medium Priority
?
846 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.

0
Comment
Question by:tdeinzer2005
4 Comments
 
LVL 14

Expert Comment

by:adwiseman
ID: 16325932
There is a server setting that can make the server case sensative.  But not an idividual query, I don't think.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16325979
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
 
LVL 10

Expert Comment

by:randeeps
ID: 16327451
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
 
LVL 3

Accepted Solution

by:
J2811 earned 2000 total points
ID: 16329953
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question