case sensitivity in Transact-SQL statement

Posted on 2006-03-29
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.

Question by:tdeinzer2005
    LVL 14

    Expert Comment

    There is a server setting that can make the server case sensative.  But not an idividual query, I don't think.
    LVL 68

    Expert Comment

    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))

    LVL 10

    Expert Comment

    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

    LVL 3

    Accepted Solution

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now