Friday, December 6, 2013

Match Case sensitive String in SQL Server

How do you compare strings so that the comparison is true only if the cases of each of the strings are equal as well. For example:

Select * from a_table where attribute = 'a'

...will return a row with an attribute of 'A'. I do not want this behavior.
So, we can write.............

Select * from a_table where attribute = 'a' COLLATE Latin1_General_CS_AS

You can also convert that attribute as case sensitive using this syntax :

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CS_AS

Now your search will be case sensitive.
If you want to make that column case insensitive again, then use

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)

COLLATE SQL_Latin1_General_CP1_CI_AS

No comments:

Post a Comment