Case Sensitive SQL Query Search
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 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.
Reference : Pinal Dave (http://blog.SQLAuthority.com)












This is good. I saved it.
thanku pinal..
it was very useful.
This helped resolve a case sensitivity issue with a DB application I was testing, a customer’s database collation was set to LATIN1_GENERAL_BIN which prevented our DB application from running it’s case variable statements thanks! :)
Hey,,
Really it’s good men………
Very Helpful too…..
its very helpful
Thanks
Regards
Ragesh
Dear Pinal
its not working, im using sql server 2005 and entered the below information:
SELECT Col2
FROM Table_1
WHERE Col2 COLLATE Latin1_General_CS_AS = ‘saeed’
but its give me err :
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘‘’.
Hi,
try like this
SELECT Col2
FROM Table_1
WHERE Col2 COLLATE Latin1_General_CS_AS = ‘saeed’
Hi Saeed
Try this
SELECT Col2
FROM Table_1
WHERE Col2 COLLATE Latin1_General_CS_AS = ‘saeed’
It is becuase of the wrong representation of the single quote
Copy the code and use single quote aound the value
Thanks so much for the help… this was excactly what I needed.
Had I just found this page first instead… :)
Hi Dave,
Thank you very much for this help.This is the thing i needed.
Thank you once again
Thanks. Its working with SQLServer 2000
thanks brother
for your blogs
v. handy.
thanx Pinal
Hi,
I am working on an application where end user will define his own collation. Can collation be specified on fly without using sp_executesql function.
No it is not possible you need to use dynamic sql anyway
Brilliant! Thanks for finding this and getting out here where I could find it.
And did Saeed figure the problem was the wrong character for quoting text (‘saeed’ vs. ‘saeed’)?
You rock. THANKS. You solved what I was being told couldn’t really be solved. Excellent!
very helpful….thank you very much.
Thanks, Pinal.
It is a very useful piece of information.
Thanks for that useful page.
very useful. thanks!
it is to much helpful for me
Thanks
it is to much helpful for me
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…
EXEC sp_helpdb DatabaseName
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
it’s realy useful and helpful for me .thax to u
very useful & helpful as well!!
Very Helpful.Thanks..You are Awesome !
Thanks resolved a problem I had with loading data from an Oracle db.
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?
That should not happen
Are you sure you have queried the correct table name?
thanks
Hey,
You are really genius…
Thanks man.
[...] Width sensitivity – A single-byte character (half-width) and the same character represented as a double-byte character (full-width) are treated differently than it is width sensitive. (Read More Here) [...]
Its really helpful for me….. Thanks a lot
loved it to the point no wasting anyone’s time thank you.
Its in my reference collection.
Thanks
Your notes is Very usefull
hi,
thanks.
this helps me as well.
thanks.
You da man! It worked.
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?
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
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.
EXEC sp_help ‘table_name’
will show you table informations where one of the columns is collation
Really very helpful…….. Thanks
It helped me alot.. Thanks.
gud one.thanq really helping alot.
Thanks so much for the help! I used the above advice and it made life so much easier!
hi Pinal,
the stuff was useful.
Thanks & Regards
Sandesh Bhawke
Thanks a lotttttt………..u saved my atleast 4 hours.
Thankz, Thankz a lot, this is very useful
How to create a column as case sensitive while creating a new table?
Use CASE SENSITIVE collations like Latin1_General_CS_AS when defining the column
ex
declare @t table(c varchar(100) collate Latin1_General_CS_AS)
Thank you. Time and again I have found your website and blogs very valuable and helpful. I appreciate your efforts.
I got cleared this concept very first time i am a beginer in sql.
Thax for ur kind suggestion.
I tried using this example with a like and wildcards, but it still produced results of all data, both upper case and lower case:
SELECT PropertyState
FROM Property
WHERE PropertyState COLLATE Latin1_General_CS_AS like ‘%[a-z]%’
I just need to find all states with parts of the state text not all uppercase. (i.e. Ct, uT, ca, Id, hI, wa)
I just found my answer:
…COLLATE Latin1_Genral_bin like ‘%[a-z]%’
Hy,
Im one question
Wat is KS and WS (French_CI_AS_KS_WS) ?
Regards
Edit Paszka
hi pinaldave,
thank you, this is very useful
It helped to resolve my problem.Thank you.
Its in SQL and not LINQ
GOOD ANSWER…..This code Help me
It made simple what others reported to be quite complicate to deal with (cross db query with different collations).
It helped me, thanks a lot,
Ste
really useful always.keen to join community
Thank you very much for a help.
Hi,
How to change the collation of database to Indic_General_90_CS_AS?
I tried out
sample is my database.
ALTER DATABASE sampleCOLLATE Indic_General_90_CS_AS
while executing this command i m getting error like
Collation ‘Indic_General_90_CS_AS’ is supported on Unicode data types only and cannot be set at the database or server level.
Can you help me!!!!!!
Regards,
Poongodi
really useful, thank
hi……
It is very useful.
Thanks
Hello Pinal and Everyone,
I am a teacher in London but would like to switch my career to sql programmer as I want to be database developer. I have done MCA and just recently passed my MCTS in sql2005. I am trying hard to get job as a sql programmar. I am applying here many jobs a day but they need experience.I always read your posts on the website.
can anybody help by giving me some voluntry work by that I can get experience.
I will very thankful to you.
waiting for your reply
Hi,
We have a similar situation recently and this is very useful. Will there be any impact on the performance if I use collate in where clause?
Most likely index will not be used
Thanks a lot for the Collate Query. Shared with my colleagues.
Very nice thing I’ve ever found in my Programming Life…. Thanks Bro…
Md. Sumonur Rahman
Dot Net Programmer
Bangladesh
Thanks verymuch.. Its very useful
thanks, its really helpful
You rock!
[...] Width sensitivity – When a single-byte character (half-width) and the same character represented as a double-byte character (full-width) are treated differently, it is width sensitive. (Read more here) [...]
Hey, its really good ya…
Thank you, nice solution
Thanks very neatly explained.
thank you it helped me to clarify my doubt
Really helpful. saved my time. Thanks.
HI ,I m facing a problem in SQL server plz guide me in that…
I m having a table in which a primary key is there with 2 columns(CODE nvarchar,VALUE nvarchar).This table contains the values in the Key columns as (X8900,A) but when I try to insert a new value as (X8900,a) ,its giving error message “primary key violation”.
Why its giving this error,if case is different for values column?
(Y) Good one….
Thank you so much ! ! ! ! ! !
Just great as usual. I used SQL_Latin1_General_Cp1253_CI_AI instead, so I do not care about capital letters or accents. Before I was using the typical replace instruction but performance began to be affected with big sets
really amazing sir …
awesome……
This made my day. Thanks PD
Hi,
How does it impact the performance by using collate clause in where conditions of the query?
How does this differs from using upper or lower functions on both sides of where clause conditions of the query?
Very helpful! Thanks!
thanks for the useful info!!!
Really a good article
Hi Sir,
SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS LIKE ‘[A-Z]%’
th above query is not returning only the value statrs with capital letters why
great work
very good job sir,its working.vijay sharma
while installing SQL2008R2 i have selected collation as case insensitive and now i would like to make case sensitive but HOW?
This page helped me, but I had a different reason for needing it. The field in the table is already collated, which was preventing me from concatenating it with another string field.
This statement fails:
SELECT [ClassId]
,[Description]
,ClassId + ‘ ‘+ Description as Combo
FROM [inventory_class]
because ClassID is a collated field in the structure, but using the collate in the select statement allowed a concatenated combination of fields to be used as parameter list in a report:
This one works:
SELECT [ClassId]
,[Description]
,ClassId COLLATE Latin1_General_CS_AS + ‘ ‘+ Description as Combo
FROM [inventory_class]
Thank you so much dear…
[...] Collate – Case Sensitive SQL Query Search [...]
Thanks and keep it up sir
It helped me
[...] Collate – Case Sensitive SQL Query Search Here is a quick script which does case sensitive SQL Query Search in the database. [...]
This code helped me, thanks.
SQL_Latin1_General_CS_AS was invalid.
I used SQL_Latin1_General_CP1_CS_AS.