SQL LIKE is Case Sensitive?

5

When I had this doubt, I did not find quick results in Portuguese that offered a response (most of the results are in StackOverflow in English).

It would be interesting to have an objective answer here in Portuguese to help the community.

  • I wonder if SQL LIKE is case sensitive?
  • Is it possible to perform case insensitive search (if it is sensitive) using SQL LIKE?
  • If you search for a name with emphasis it is possible that it identifies that "Luis" is related to "Luis" and vice versa?
asked by anonymous 23.03.2017 / 16:46

2 answers

5
  

I wonder if SQL LIKE is case sensitive?

The command LIKE only compares string according to a previous pattern. Who is responsible for the case (in) sensitive is collation .

  

Is it possible to perform case insensitive search (if it is sensitive) using SQL LIKE?

Yes, depending only on the collation configured.

  

If you search for a name with an accent, can it identify that "Luis" is related to "Luis" and vice versa?

Yes, depending only on the collation configured.

As this answer on What is the difference between charset and collation in database? , understand what is collation :

  

Collation

     

Collation is the term used to define the set of rules that the   server will use for sorting and comparing between texts, or   either, as will the operation of the operators =, & gt ;, & lt ;, order by, etc.   For example, depending on the Collation configured, the system will   sort the 'ö' character between the 'o' and 'p' characters using another   collation, this character can be sorted in another position. Therefore   can conflict when making queries that relate tables with   different collations. In addition, the collation also defines whether the   system will differentiate accented characters or if it will be case   sensitive, for example the Latin1_General_CI_AS collation defines that the   system should treat the characters as case insensitive (CI) and   accentue sensitive (AS). Examples:

     

latin1_general_ci : There is no distinction between uppercase and   lowercase Searching for "test", records such as "Test" or "TEST"   will be returned.

     

latin1_general_cs : Distinguish between uppercase and   lowercase Searching for "test" will only return "test". options   such as "Test" and "TEST" will not be returned.

     

latin1_swedish_ci : No   distinguish upper and lower case letters and neither accented characters nor   with a cedilla, that is, the record containing the word "Intuition" will be   returned when there is a search for the word "int"

    
24.03.2017 / 12:49
-1

In MySQL the LIKE operator is case-insensitive by default, whether you're looking for Ze, Ze, Z or Z.

If you compare Luis and Luis's accent you can simply use an OR, for example:

SELECT * FROM <tabela>
WHERE nome LIKE 'Luís' OR nome LIKE 'Luís'

More information can be found in the MySQL documentation .

    
24.03.2017 / 04:28