Home > Database > Mysql Tutorial > How Can I Perform Case-Sensitive String Comparisons in SQL?

How Can I Perform Case-Sensitive String Comparisons in SQL?

DDD
Release: 2025-01-11 14:07:43
Original
552 people have browsed it

How Can I Perform Case-Sensitive String Comparisons in SQL?

Achieving Case-Sensitive String Comparisons in SQL

Standard SQL string comparisons often ignore case differences. A query like SELECT * FROM table WHERE column = 'value' will find matches regardless of capitalization.

Enforcing Case Sensitivity

To perform a case-sensitive comparison, utilize the COLLATE clause. This clause specifies the collation—the set of rules governing string sorting and comparison. Selecting a case-sensitive collation ensures accurate case-sensitive matching.

Illustrative Example

Imagine a table with a string attribute:

<code>| attribute |
|---|---|
| abc |
| ABC |
| aBc |</code>
Copy after login

A typical query:

<code class="language-sql">SELECT * FROM table WHERE attribute = 'ABC';</code>
Copy after login

...would return all three rows due to case-insensitive matching.

To enforce case sensitivity, use COLLATE:

<code class="language-sql">SELECT * FROM table WHERE attribute = 'ABC' COLLATE Latin1_General_CS_AS;</code>
Copy after login

Latin1_General_CS_AS is a case-sensitive collation. This revised query will only return the row where attribute is precisely 'ABC'.

The above is the detailed content of How Can I Perform Case-Sensitive String Comparisons in SQL?. 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