Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.0 » SQL Anywhere Server - SQL Reference » Using SQL » SQL language elements » Search conditions » LIKE, REGEXP, and SIMILAR TO search conditions


SIMILAR TO search condition

Match a pattern against a string.

expression [ NOT ] SIMILAR TO pattern [ ESCAPE escape-expression ]

expression   The expression to be searched.

pattern   The regular expression to search for within expression.

For more information about the supported syntax for regular expressions, see Regular expressions.

escape-expression   The escape character to use in the match. The default is the NULL byte.

Regular expression syntax Meaning
\x Match anything that compares equal to x, where the escape character is assumed to be the backslash character (\). For example, \[ matches '['.
x Any character (other than a meta-character) matches itself. For example, A matches 'A'.

To match a substring with the string, use the percentage sign wildcard (%expression). For example, SELECT ... WHERE Description SIMILAR TO 'car' matches only car, not sportscar. However, SELECT ... WHERE Description SIMILAR TO '%car' matches car, as well as sportscar, and any string that ends with car. Alternatively, you can rewrite your query to make use the REGEXP_SUBSTR function, which is designed to search for substrings within a string.

When matching against a sub-character class in an empty larger character class, remember to include the outer square brackets, as well as the square brackets for the sub-character class (for example, expression SIMILAR TO '[[:digit:]]'). For more on sub-character class matching, see Regular expressions: Special sub-character classes.

How comparisons are performed

Comparisons are performed character-by-character, unlike the equivalence (=) operator and other operators where the comparison is done string-by-string. For example, when a comparison is done in a UCA collation (CHAR or NCHAR with the collation set to UCA), 'Æ'='AE' is true, but 'Æ' SIMILAR TO 'AE' is false.

For a character-by-character comparison to match, each single character in the expression being searched must match a single character (using the collation's character equivalence), or a wildcard in the SIMILAR TO pattern.

National character (NCHAR) support

SIMILAR TO search conditions can be used to compare CHAR and NCHAR strings. In this case, character set conversion is performed so that the comparison is done using a common data type. Then, a character-by-character comparison (or code point by code point, in the case of UTF8 databases) is performed. See Comparisons between CHAR and NCHAR.

You can specify expression or pattern as an NCHAR string literal by prefixing the quoted value with N (for example, expression SIMILAR TO N'pattern'). You can also use the CAST function to cast the pattern to CHAR or NCHAR (for example, expression SIMILAR TO CAST(pattern AS datatype).

See String literals, and CAST function [Data type conversion].

See also