Home > Database > Mysql Tutorial > How to Perform Case-Sensitive Searches in SQL Server's WHERE Clause?

How to Perform Case-Sensitive Searches in SQL Server's WHERE Clause?

DDD
Release: 2025-01-14 06:35:45
Original
255 people have browsed it

How to Perform Case-Sensitive Searches in SQL Server's WHERE Clause?

Achieving Case-Sensitive Searches in SQL Server's WHERE Clause

Standard SQL Server queries are inherently case-insensitive when comparing strings. To perform a case-sensitive search, several techniques are available.

Approach 1: Adjusting Collation Settings

The underlying collation setting dictates the case sensitivity of string comparisons. By changing the collation to a case-sensitive option, you can enforce case-sensitive behavior within your query. Example:

<code class="language-sql">SELECT 1
FROM dbo.Customers
WHERE   CustID = @CustID COLLATE SQL_Latin1_General_CP1_CS_AS
AND     OrderID = @OrderID COLLATE SQL_Latin1_General_CP1_CS_AS</code>
Copy after login

Approach 2: Modifying Column Attributes

Alternatively, you can modify the properties of the columns participating in the search to make them case-sensitive. This is a schema-level change affecting all queries using those columns.

Approach 3: Utilizing LIKE with Collation

The LIKE operator can also be combined with COLLATE to achieve case-sensitive matching:

<code class="language-sql">SELECT *
FROM tbl_Partners
WHERE PartnerName COLLATE SQL_Latin1_General_CP1_CS_AS LIKE 'MyEx%' COLLATE SQL_Latin1_General_CP1_CS_AS</code>
Copy after login

The above is the detailed content of How to Perform Case-Sensitive Searches in SQL Server's WHERE Clause?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template