MySQL Cookbook Free Open Book

MySQL Cookbook

Previous Section Next Section

4.11 Controlling Case Sensitivity in Pattern Matching

4.11.1 Problem

A pattern match is case sensitive when you don't want it to be, or vice versa.

4.11.2 Solution

Alter the case sensitivity of the strings.

4.11.3 Discussion

By default, LIKE is not case sensitive:

mysql> SELECT name, name LIKE '%i%', name LIKE '%I%' FROM metal;
+----------+-----------------+-----------------+
| name     | name LIKE '%i%' | name LIKE '%I%' |
+----------+-----------------+-----------------+
| copper   |               0 |               0 |
| gold     |               0 |               0 |
| iron     |               1 |               1 |
| lead     |               0 |               0 |
| mercury  |               0 |               0 |
| platinum |               1 |               1 |
| silver   |               1 |               1 |
| tin      |               1 |               1 |
+----------+-----------------+-----------------+

Currently, REGEXP is not case sensitive, either.

mysql> SELECT name, name REGEXP 'i', name REGEXP 'I' FROM metal;
+----------+-----------------+-----------------+
| name     | name REGEXP 'i' | name REGEXP 'I' |
+----------+-----------------+-----------------+
| copper   |               0 |               0 |
| gold     |               0 |               0 |
| iron     |               1 |               1 |
| lead     |               0 |               0 |
| mercury  |               0 |               0 |
| platinum |               1 |               1 |
| silver   |               1 |               1 |
| tin      |               1 |               1 |
+----------+-----------------+-----------------+

However, prior to MySQL 3.23.4, REGEXP operations are case sensitive:

mysql> SELECT name, name REGEXP 'i', name REGEXP 'I' FROM metal;
+----------+-----------------+-----------------+
| name     | name REGEXP 'i' | name REGEXP 'I' |
+----------+-----------------+-----------------+
| copper   |               0 |               0 |
| gold     |               0 |               0 |
| iron     |               1 |               0 |
| lead     |               0 |               0 |
| mercury  |               0 |               0 |
| platinum |               1 |               0 |
| silver   |               1 |               0 |
| tin      |               1 |               0 |
+----------+-----------------+-----------------+

Note that the (current) behavior of REGEXP not being case sensitive can lead to some unintuitive results:

mysql> SELECT 'a' REGEXP '[[:lower:]]', 'a' REGEXP '[[:upper:]]';
+--------------------------+--------------------------+
| 'a' REGEXP '[[:lower:]]' | 'a' REGEXP '[[:upper:]]' |
+--------------------------+--------------------------+
|                        1 |                        1 |
+--------------------------+--------------------------+

Both expressions are true because [:lower:] and [:upper:] are equivalent when case sensitivity doesn't matter.

If a pattern match uses different case-sensitive behavior than what you want, control it the same way as for string comparisons:

  • To make a pattern match case sensitive, use a binary string for either operand (for example, by using the BINARY keyword). The following query shows how the non-binary column name normally is not case sensitive:

    mysql> SELECT name, name LIKE '%i%%', name REGEXP 'i' FROM metal;
    +----------+------------------+-----------------+
    | name     | name LIKE '%i%%' | name REGEXP 'i' |
    +----------+------------------+-----------------+
    | copper   |                0 |               0 |
    | gold     |                0 |               0 |
    | iron     |                1 |               1 |
    | lead     |                0 |               0 |
    | mercury  |                0 |               0 |
    | platinum |                1 |               1 |
    | silver   |                1 |               1 |
    | tin      |                1 |               1 |
    +----------+------------------+-----------------+

    And this query shows how to force name values to be case sensitive using BINARY:

    mysql> SELECT name, BINARY name LIKE '%I%', BINARY name REGEXP 'I' FROM metal;
    +----------+------------------------+------------------------+
    | name     | BINARY name LIKE '%I%' | BINARY name REGEXP 'I' |
    +----------+------------------------+------------------------+
    | copper   |                      0 |                      0 |
    | gold     |                      0 |                      0 |
    | iron     |                      0 |                      0 |
    | lead     |                      0 |                      0 |
    | mercury  |                      0 |                      0 |
    | platinum |                      0 |                      0 |
    | silver   |                      0 |                      0 |
    | tin      |                      0 |                      0 |
    +----------+------------------------+------------------------+

    Using BINARY also has the effect of causing [:lower:] and [:upper:] in regular expressions to act as you would expect. The second expression in the following query yields a result that really is true only for uppercase letters:

    mysql> SELECT 'a' REGEXP '[[:upper:]]', BINARY 'a' REGEXP '[[:upper:]]';
    +--------------------------+---------------------------------+
    | 'a' REGEXP '[[:upper:]]' | BINARY 'a' REGEXP '[[:upper:]]' |
    +--------------------------+---------------------------------+
    |                        1 |                               0 |
    +--------------------------+---------------------------------+
  • A pattern match against a binary column is case sensitive. To make the match not case sensitive, make both operands the same lettercase. To see how this works, modify the metal table to add a binname column that is like the name column except that it is VARCHAR BINARY rather than VARCHAR:

    mysql> ALTER TABLE metal ADD binname VARCHAR(20) BINARY;
    mysql> UPDATE metal SET binname = name;

    The first of the following queries shows how the binary column binname normally is case sensitive in pattern matches, and the second shows how to force it not to be, using UPPER( ):

    mysql> SELECT binname, binname LIKE '%I%', binname REGEXP 'I'
        -> FROM metal;
    +----------+--------------------+--------------------+
    | binname  | binname LIKE '%I%' | binname REGEXP 'I' |
    +----------+--------------------+--------------------+
    | copper   |                  0 |                  0 |
    | gold     |                  0 |                  0 |
    | iron     |                  0 |                  0 |
    | lead     |                  0 |                  0 |
    | mercury  |                  0 |                  0 |
    | platinum |                  0 |                  0 |
    | silver   |                  0 |                  0 |
    | tin      |                  0 |                  0 |
    +----------+--------------------+--------------------+
    mysql> SELECT binname, UPPER(binname) LIKE '%I%', UPPER(binname) REGEXP 'I'
        -> FROM metal;
    +----------+---------------------------+---------------------------+
    | binname  | UPPER(binname) LIKE '%I%' | UPPER(binname) REGEXP 'I' |
    +----------+---------------------------+---------------------------+
    | copper   |                         0 |                         0 |
    | gold     |                         0 |                         0 |
    | iron     |                         1 |                         1 |
    | lead     |                         0 |                         0 |
    | mercury  |                         0 |                         0 |
    | platinum |                         1 |                         1 |
    | silver   |                         1 |                         1 |
    | tin      |                         1 |                         1 |
    +----------+---------------------------+---------------------------+
    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Using the mysql Client Program
    Chapter 2. Writing MySQL-Based Programs
    Chapter 3. Record Selection Techniques
    Chapter 4. Working with Strings
    4.1 Introduction
    4.2 Writing Strings That Include Quotes or Special Characters
    4.3 Preserving Trailing Spaces in String Columns
    4.4 Testing String Equality or Relative Ordering
    4.5 Decomposing or Combining Strings
    4.6 Checking Whether a String Contains a Substring
    4.7 Pattern Matching with SQL Patterns
    4.8 Pattern Matching with Regular Expressions
    4.9 Matching Pattern Metacharacters Literally
    4.10 Controlling Case Sensitivity in String Comparisons
    4.11 Controlling Case Sensitivity in Pattern Matching
    4.12 Using FULLTEXT Searches
    4.13 Using a FULLTEXT Search with Short Words
    4.14 Requiring or Excluding FULLTEXT Search Words
    4.15 Performing Phrase Searches with a FULLTEXT Index
    Chapter 5. Working with Dates and Times
    Chapter 6. Sorting Query Results
    Chapter 7. Generating Summaries
    Chapter 8. Modifying Tables with ALTER TABLE
    Chapter 9. Obtaining and Using Metadata
    Chapter 10. Importing and Exporting Data
    Chapter 11. Generating and Using Sequences
    Chapter 12. Using Multiple Tables
    Chapter 13. Statistical Techniques
    Chapter 14. Handling Duplicates
    Chapter 15. Performing Transactions
    Chapter 16. Introduction to MySQL on the Web
    Chapter 17. Incorporating Query Resultsinto Web Pages
    Chapter 18. Processing Web Input with MySQL
    Chapter 19. Using MySQL-Based Web Session Management
    Appendix A. Obtaining MySQL Software
    Appendix B. JSP and Tomcat Primer
    Appendix C. References
    Colophone
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele