Home > Database > Mysql Tutorial > How to Perform Case-Insensitive String Comparisons in SQL Server?

How to Perform Case-Insensitive String Comparisons in SQL Server?

Mary-Kate Olsen
Release: 2024-12-30 05:08:09
Original
186 people have browsed it

How to Perform Case-Insensitive String Comparisons in SQL Server?

Case-Insensitive Queries in SQL Server

In SQL Server, string comparisons are typically case-sensitive. However, if you need to perform a case-insensitive comparison in a WHERE clause, you can use the COLLATE operator.

Example:

Suppose you have the following data in a table called myTable:

| myField |
|---------|
| sOmeVal |
| AnOtHeRVaL |
Copy after login

The following query selects all rows where the myField column is equal to sOmeVal in a case-insensitive manner:

SELECT * FROM myTable WHERE myField COLLATE SQL_Latin1_General_CP1_CI_AS = 'sOmeVal';
Copy after login

The COLLATE operator takes two arguments:

  • The expression to be compared
  • The collation to be used for the comparison

The collation specifies the rules for comparing strings. In this example, we are using the SQL_Latin1_General_CP1_CI_AS collation, which performs case-insensitive comparisons.

Note that the default collation for your database may be different. You can check the default collation using the following query:

SELECT COLLATION_NAME FROM sys.databases WHERE name = 'myDatabase';
Copy after login

If the default collation is not case-insensitive, you will need to specify the collation explicitly in your queries.

The above is the detailed content of How to Perform Case-Insensitive String Comparisons in SQL Server?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template