Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / ATL
Technical Blog

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 93.3K   3   8
How to do case sensitive string match in SQL Server

Introduction

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.

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

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.

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

Keep learning and sharing! Cheers!

License

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


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

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. #SOreadytohelp

Comments and Discussions

 
GeneralMy vote of 4 Pin
Umesh AP21-Mar-17 23:35
Umesh 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 Kejriwal10-Sep-14 5:55
professionalNitesh Kejriwal10-Sep-14 5:55 
GeneralMy vote of 2 Pin
Rajesh Buddaraju22-May-14 0:07
Rajesh Buddaraju22-May-14 0:07 
GeneralRe: My vote of 2 Pin
RichardJackson5-May-15 13:55
RichardJackson5-May-15 13:55 
QuestionPassword? Pin
dandy7212-May-14 13:58
dandy7212-May-14 13:58 
AnswerRe: Password? Pin
Nitesh Kejriwal12-May-14 18:43
professionalNitesh Kejriwal12-May-14 18:43 
GeneralRe: Password? Pin
dandy7213-May-14 2:27
dandy7213-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.