Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a case sensitive search in WHERE clause (I'm using SQL Server)?

Tags:

sql

sql-server

I want to do a case sensitive search in my SQL query. But by default, SQL Server does not consider the case of the strings.

Any idea on how to do a case sensitive search in SQL query?

like image 585
Veera Avatar asked Dec 02 '09 06:12

Veera


People also ask

Is WHERE clause in SQL case sensitive?

1 Answer. A case sensitive search in WHERE clause can be done via changing the Collation. Because, by default, it is case insensitive.

How do I use a case statement in the WHERE clause in SQL Server?

The CASE statement returns the value based on condition. We can use a case statement in Where, Order by and Group by clause. In the Customer table, I have displayed the First Name is Ram or the Last Name is Sharma's salary. So, by using a CASE statement with the where condition displays the result.

How do you match case sensitive in SQL Server?

Let us consider below is our normal SQL procedure that validates a user from the tblUser table. 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. Keep learning and sharing!


1 Answers

Can be done via changing the Collation. By default it is case insensitive.

Excerpt from the link:

SELECT 1 FROM dbo.Customers WHERE   CustID = @CustID COLLATE SQL_Latin1_General_CP1_CS_AS     AND CustPassword = @CustPassword COLLATE SQL_Latin1_General_CP1_CS_AS 

Or, change the columns to be case sensitive.

like image 95
Ashish Jain Avatar answered Oct 13 '22 06:10

Ashish Jain