Tuesday, September 4, 2012

Case Sensitive SQL Query Search

Have you ever noticed when you do a string comparison in SQL using '=' notation, case of the string is neglected ???
For example, If TestTable has a TestColumn and it consist of  values ‘CaseSearch, casesearch, CASESEARCH, CaSeSeArCh’, following statement will return you all the four records.

SELECT Column1
FROM Table1
WHERE Column1 = 'casesearch'

If you dont want to ignore case and return only the exact row with the specified value, the query should be modified as follows,

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

The above change should be done since the default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS is not case sensitive.

No comments:

Post a Comment