Click here to Skip to main content
15,031,941 members
Articles / Desktop Programming / ATL
Technical Blog
Posted 12 May 2014

Tagged as


3 bookmarked

How To do Case Sensitive String Match in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.43/5 (7 votes)
12 May 2014CPOL
How to do case sensitive string match in SQL Server


While working on a SP today, I noticed that SQL Server does a case-insensitive string match in a query. Hence in scenarios where passwords are to be validated, using a query as “WHERE Password =@Password” will give valid results if the user enters password as “admin” or “ADMIN” even though when the password is set as “aDmiN”. In this post, we will resolve this issue using a very simple method.

Let us consider below is our normal SQL procedure that validates a user from the tblUser table.

CREATE PROCEDURE [dbo].[tblUserSelect_Authenticate]
    @Username nvarchar(50),
    @Password nvarchar(50)
    SELECT * FROM tblUser WHERE Username = @Username AND Password = @Password

To tell SQL do a case-sensitive search, we will modify the above procedure as below. Please note we added a “COLLATE SQL_Latin1_General_CP1_CS_AS” to the field we want an exact match for.

CREATE PROCEDURE [dbo].[tblUserSelect_Authenticate]
    @Username nvarchar(50),
    @Password nvarchar(50)
     SELECT * FROM tblUser WHERE Username = @Username AND Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS

Keep learning and sharing! Cheers!


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


About the Author

Nitesh Luharuka
Team Leader Director @ Rebin Infotech
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.

I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.

Comments and Discussions

GeneralMy vote of 4 Pin
Umesh AP21-Mar-17 23:35
MemberUmesh AP21-Mar-17 23:35 
GeneralMy vote of 5 Pin
MarkBoreham10-Sep-14 4:30
professionalMarkBoreham10-Sep-14 4:30 
GeneralRe: My vote of 5 Pin
Nitesh Luharuka10-Sep-14 5:55
professionalNitesh Luharuka10-Sep-14 5:55 
GeneralMy vote of 2 Pin
Rajesh Varma Buddaraju22-May-14 0:07
MemberRajesh Varma Buddaraju22-May-14 0:07 
GeneralRe: My vote of 2 Pin
RichardJackson5-May-15 13:55
MemberRichardJackson5-May-15 13:55 
QuestionPassword? Pin
dandy7212-May-14 13:58
Memberdandy7212-May-14 13:58 
AnswerRe: Password? Pin
Nitesh Luharuka12-May-14 18:43
professionalNitesh Luharuka12-May-14 18:43 
GeneralRe: Password? Pin
dandy7213-May-14 2:27
Memberdandy7213-May-14 2:27 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.