SQL SERVER – Collate – Case Sensitive SQL Query Search

In this blog post we are going to learn about how to do Case Sensitive SQL Query Search.

SQL SERVER - Collate - Case Sensitive SQL Query Search casesensitive

If Column1 of Table1 has following values ‘CaseSearch, casesearch, CASESEARCH, CaSeSeArCh’, following statement will return you all the four records.

SELECT Column1
FROM Table1
WHERE Column1 = 'casesearch'

To make the query case sensitive and retrieve only one record (“casesearch”) from the above query, the collation of the query needs to be changed as follows.

SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS = 'casesearch'

Adding COLLATE Latin1_General_CS_AS makes the search case sensitive.

Default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS is not case sensitive.

To change the collation of the any column for any table permanently run following query.

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(20)
COLLATE Latin1_General_CS_AS

To know the collation of the column for any table run following Stored Procedure.

EXEC sp_help DatabaseName

Second results set above script will return you collation of database DatabaseName.

Here are few of the related blog posts on the same subject:

Let me know your feedback about this blog post in the comments section.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Collation, SQL Scripts, SQL Search, SQL Server, SQL Utility
Previous Post
SQL SERVER – FIX : ERROR : Msg 3159, Level 16, State 1, Line 1 – Msg 3013, Level 16, State 1, Line 1
Next Post
SQLAuthority.com 100th Post – Gratitude Note to Readers

Related Posts

120 Comments. Leave new

  • EXEC sp_help DatabaseName … does not work

    i replaced my database name above….it gave me error saying….
    The object ‘aim0rodb’ does not exist in database ‘aim0rodb’.
    where ‘aim0rodb’ is my database name…

    any help……..

    Thanks in advance…

    Reply
  • You don’t need to give DatabaseName in EXEC sp_help if your object explorer already selected a database by default

    You can give TableName in place of DatabaseName if data base is selected like EXEC sp_help

    Reply
  • it’s realy useful and helpful for me .thax to u

    Reply
  • very useful & helpful as well!!

    Reply
  • Very Helpful.Thanks..You are Awesome !

    Reply
  • Thanks resolved a problem I had with loading data from an Oracle db.

    Reply
  • I have updated a table’s length property using Alter table. However when running sp_help TableName it displays the old value for the length. How do I update this information?

    Reply
  • thanks

    Reply
  • Hey,

    You are really genius…

    Thanks man.

    Reply
  • Its really helpful for me….. Thanks a lot

    Reply
  • loved it to the point no wasting anyone’s time thank you.
    Its in my reference collection.

    Reply
  • Thanks
    Your notes is Very usefull

    Reply
  • hi,
    thanks.
    this helps me as well.
    thanks.

    Reply
  • You da man! It worked.

    Reply
  • Hi,
    the material excellent on the website ……….appreciate ure help …..
    I want to know
    how to make an existing DB (that contains schema) case-insensitive ,is there a way to make the DB as well as the schema case-insensitive?

    Reply
  • Hello Ruchi,

    If the database is case sensitive then schema identfiers are case sensitive in t-sql code. For example if a database name TestDB has a table tblTEST then following statement runs successfully:

    SELECT * FROM tblTEST

    But below statement fails:

    SELECT * FROM tbltest

    You can change the collation of database by alter database statement as below:

    ALTER DATABASE
    COLLATE SQL_Latin1_General_CP1_CI_AS

    After this the the schema identifiers in t-sql statements are case insensitive and above written both SELECT statements run successfully. But one more thing to consider here is that changing the collation of database does not change the collation of existing objects in that database. New collation will applied as default collation for all objects that are created after collate change.

    Regards,
    Pinal Dave

    Reply
  • I altered my table successfully, but when I select something from that table and group by the specific field, it doesn’t work. Even when I run sp_help it doesn’t show any collation.

    Reply
    • EXEC sp_help ‘table_name’

      will show you table informations where one of the columns is collation

      Reply
  • Really very helpful…….. Thanks

    Reply
  • It helped me alot.. Thanks.

    Reply
  • gud one.thanq really helping alot.

    Reply

Leave a Reply