Click here to Skip to main content
Click here to Skip to main content

Case Sensitive search with SQL

By , 27 Dec 2011
 

Background

Last week our senior told us to select records from database starts with 'r' (small R). it troubles a lot, but we got solution finally.

Many applications have a functional requirement for the located database to have a case-sensitive search or sort-order, that character data related operations are case-sensitive.

may be there are some situation where you need to find case sensitive records from database.

How to do that ?

To achive this task we need to take help of SQL Collation.

What is SQL Collation ?

MSDN Says:

"Is a clause that can be applied to a database definition or a column definition to define the collation, or to a character string expression to apply a collation cast."

A collation encodes the rules governing the proper use of characters for either a language, such as Greek or Polish, or an alphabet such as Latin1_General (the Latin alphabet used by western European languages).

In simple words we bring together all rules and applied it on table or column defination. default collation is for SQL is Latin.

Get into action

suppose i have a table named 'users' contains 4 records.
1. responsive
2. Responsive
3. RESPONSIVE
4. ResPONsive


If we fire following Query,
"select * from users where name = 'responsive'"
it will return us all columns. Cause, all column contains same data and record search is not case Sensitive.

following image clear idea,

select.GIF

To fetch case sensitive records you need to change collation of that column.

Change column collation

Default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS and this is not case sensitive. here is syntax to change column collation.

SELECT Column1 FROM Table1 WHERE Column1 = 'expression'

here we go

collate.GIF

Yes. we gor exact result.

by above method, we change column collate for temporary use. but we can change it's collate permanently. by using 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 tableName

here is the result

StoredProcedure.GIF

Thanks

This simple and short article gives you a suggestion "How to do casesensitive search in database",

Suggestion are most welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

koolprasad2003
Software Developer
India India
Member
No one play your role BEST.... than U

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberElham M21 Aug '12 - 20:33 
It's cool.that's one that I want
GeneralMy vote of 3memberKP Lee2 Jan '12 - 21:44 
I agree with the other posters that this is really basic SQL information and so simple it should have been in the tip section.
GeneralMy vote of 3membertecgoblin2 Jan '12 - 20:29 
You should indeed post this as a tip
GeneralMy vote of 4memberSuvendu Shekhar Giri28 Dec '11 - 9:37 
nice explaination
QuestionPost it as a tipmemberShameel28 Dec '11 - 4:08 
The scope is too narrow for an article. Consider posting it in the tips and tricks section or try expanding the scope of the article to include general search in SQL Server or more collation options and how they affect search.
AnswerRe: Post it as a tipmemberkoolprasad200329 Dec '11 - 20:53 
Thanks for suggestion Shameel,
i will enhance it's scope in later versions. Smile | :)
Rating always..... WELCOME
 
The only reason people get lost in thought is because it's unfamiliar territory.

GeneralMy vote of 3membersagnik mukherjee27 Dec '11 - 18:47 
Good for Basic
GeneralMy vote of 5memberPranit Kothari27 Dec '11 - 2:53 
Thx. Useful!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 28 Dec 2011
Article Copyright 2011 by koolprasad2003
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid