Home > Database > Mysql Tutorial > How Can I Perform Case-Insensitive Wildcard Searches in SQL?

How Can I Perform Case-Insensitive Wildcard Searches in SQL?

DDD
Release: 2025-01-17 13:42:09
Original
893 people have browsed it
<code>| title           |
|-----------------|
| Elm Tree         |
| elm tree         |
| Red Elm          |
| RED ELM          |
| Oak Tree         |
</code>
Copy after login

The query SELECT * FROM trees WHERE LOWER(trees.title) LIKE '%elm%' will return the following rows:

<code>| title           |
|-----------------|
| Elm Tree         |
| elm tree         |
| Red Elm          |
| RED ELM          |</code>
Copy after login
Copy after login

This the case-insensitive nature of the search using the LOWER function. Note that this approach converts all values ​​to lowercase before comparison, which may affect performance on very large tables. Other database systems may offer alternative case-insensitive comparison methods.

How Can I Perform Case-Insensitive Wildcard Searches in SQL?

SQL case-insensitive wildcard search

Case sensitivity can be a challenge when performing wildcard searches in SQL using the LIKE operator. For example, the query SELECT * FROM trees WHERE trees.title LIKE '%elm%' will only return results if the title column contains the exact string "elm".

Solution: Use LOWER function

MySQL provides a solution to this problem using the LOWER function. By including the column in the LOWER function, the search becomes case-insensitive. Here’s how:

<code class="language-sql">SELECT * FROM trees WHERE LOWER(trees.title) LIKE '%elm%'</code>
Copy after login

In this query, the LOWER function converts the contents of the title column to lowercase before performing a LIKE search. Therefore, the query will return all rows whose lowercase representation of title contains the string "elm".

Example

Consider a hypothetical table called trees containing the following rows:

<code>| title           |
|-----------------|
| Elm Tree         |
| elm tree         |
| Red Elm          |
| RED ELM          |
| Oak Tree         |</code>
Copy after login

QuerySELECT * FROM trees WHERE LOWER(trees.title) LIKE '%elm%' will return the following rows:

<code>| title           |
|-----------------|
| Elm Tree         |
| elm tree         |
| Red Elm          |
| RED ELM          |</code>
Copy after login
Copy after login

This demonstrates the case-insensitive search feature using the LOWER function. Note that this method converts all values ​​to lowercase before comparing, which may impact performance on large tables. Other database systems may provide alternative case-insensitive comparison methods.

<code></code>
Copy after login

The above is the detailed content of How Can I Perform Case-Insensitive Wildcard Searches 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