SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column's value.
Syntax:
SELECT <select_list> FROM <table_name> WHERE <column_name> [NOT] LIKE <column_value> [ESCAPE <char>]
- Indexed Columns
- If the left side of the LIKE operator is the name of an indexed column of type string, ActiveSpaces converts the LIKE operator into a range query using >= and <. This enables the ActiveSpaces index selection algorithm to select the index and use it for scanning rows when processing the query. For example, take a look at the following statement:
SELECT * FROM table1 WHERE lastname LIKE 'Long'
Internally, this statement is converted to the following statement:SELECT * FROM table1 WHERE lastname >= 'LONG' AND lastname < 'long'
Even if we have an index on the column, using LIKE <column_value> results in a full table scan if the <column_value> is a string that starts with a wildcard (%, _) or digit, or if <column_value> is a long value. - Pattern Matching
- The pattern matching is not case sensitive for upper or lower case ASCII characters. The pattern matching is case-sensitive for Unicode characters that are beyond the ASCII range.
SELECT * FROM table1 WHERE lastname LIKE 'long'
searches the lastname column for values of LONG, lONG, loNG, lonG, long, LoNG, LonG, and so on. - Wildcards
- Two wildcard characters can be used in conjunction with the LIKE operator:
- Escape Character
- If the character sequence being matched needs to include one of the wildcard characters as one of the characters to match, you can specify an escape character to use by specifying the optional ESCAPE clause after your LIKE pattern. For example, the following statement matches values of 100%:
SELECT * FROM table1 WHERE completed LIKE '100\%' ESCAPE '\'
Related concepts
Copyright © Cloud Software Group, Inc. All rights reserved.