Prev | SQL Engine Reference | Next |
LIKE
LIKE allows pattern matching within character-based column data.
Syntax
WHERE expr [ NOT ] LIKE valueRemarks
The value on the right side of a LIKE expression must be a simple string constant, the USER keyword, or (outside a stored procedure) a dynamic parameter supplied at runtime, indicated by a question mark "?". Dynamic parameters are not supported within SQL Data Manager, only in application code.
Use the percent sign "%" in the comparison value as many times as desired to match zero or more characters in the column values. Use the underscore "_" to match any one character. If you need to match one of these wildcard symbols as a literal character, use a backslash in front of the symbol to mark the symbol as a literal character.
Examples
This example matches all column values that are five characters long and have
abc
as the middle three characters:SELECT Building_Name FROM Room WHERE Building_Name LIKE '_abc_'This example matches all column values that contain a backslash:
SELECT Building_Name FROM Room where Building_Name LIKE '%\\%'This example matches all column values except those that begin with a percent sign:
SELECT Building_Name FROM Room where Building_Name NOT LIKE '\%%'This example matches all column values that contain one or more single-quotes:
SELECT Building_Name FROM Room where Building_Name LIKE '%''%'This example matches all column values where the second character is a double-quote:
SELECT Building_Name FROM Room where Building_Name LIKE '_"%'This example creates a stored procedure that returns any rows where the
Building_Name
column contains the characters stored in the input variable:rname
and where theType
column contains the characters stored in the input variable:rtype
.CREATE PROCEDURE room_test(IN :rname CHAR(20), IN :rtype CHAR(20)) RETURNS(Building_Name CHAR(25), "Type" CHAR(20)); BEGIN DECLARE :like1 CHAR(25); DECLARE :like2 CHAR(25); SET :like1 = '%' + :rname + '%'; SET :like2 = '%' + :rtype + '%'; SELECT Building_Name, "Type" FROM Room WHERE Building_Name LIKE :like1 AND "Type" LIKE :like2; END;Note that the following statement, if placed in the stored procedure above, generates a syntax error because of the expression on the right side of the LIKE operator. The right side must be a simple constant:
SELECT Building_Name, "Type" from Room where Building_Name LIKE '%' + :rname + '%';
Prev LEAVE |
Contents Up Check for Revisions | Next LOOP |