To match a literal instance of a SQL pattern metacharacter, precede it with a backslash: : Patterns « Regular Expression « SQL / MySQL






To match a literal instance of a SQL pattern metacharacter, precede it with a backslash:

      
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  c       CHAR(1)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytable (c)
    ->  VALUES
    ->          ('%'),
    ->          ('_'),
    ->          ('.'),
    ->          ('^'),
    ->          ('$'),
    ->          ('\\')
    -> ;
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM mytable;
+------+
| c    |
+------+
| %    |
| _    |
| .    |
| ^    |
| $    |
| \    |
+------+
6 rows in set (0.00 sec)

mysql>
mysql>
mysql> SELECT c, c LIKE '\%', c LIKE '\_' FROM mytable;
+------+-------------+-------------+
| c    | c LIKE '\%' | c LIKE '\_' |
+------+-------------+-------------+
| %    |           1 |           0 |
| _    |           0 |           1 |
| .    |           0 |           0 |
| ^    |           0 |           0 |
| $    |           0 |           0 |
| \    |           0 |           0 |
+------+-------------+-------------+
6 rows in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Pattern match: string has 5 characters
2.Pattern match: letter repeats
3.Pattern in Where clause
4.Using Pattern Matching
5.Pattern Matching
6.Show records where the name matches a search pattern
7.Matching Pattern Metacharacters Literally
8.To make a pattern match case sensitive, use a binary string for either operand
9.Pattern Matching with Regular Expressions