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?
1 Answer. A case sensitive search in WHERE clause can be done via changing the Collation. Because, by default, it is case insensitive.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With