String Operators

This page summarizes string operators for streaming SQL, including concatenation and string pattern comparison. These let you combine and compare the strings.

OperatorUnary / BinaryDescriptionNotes
||BConcatenationConcatenation also applies to binary types
LIKEBString pattern comparison LIKE [ESCAPE ]
SIMILAR TOBString pattern comparison SIMILAR TO [ESCAPE ]

Concatenation

This operator is used to concatenate one or more strings:

OperationResult
'SQL'||'stream'SQLstream
'SQL'||"||'stream'SQLstream
'SQL'||"||'stream'||'Incorporated'SQLstream Incorporated
<col1>||<col2>||<col3>||<col4> <col1><col2><col3><col4>

LIKE patterns

LIKE compares a string to a string pattern. In the pattern, the characters _ (underscore) and % (percent) have special meaning:

Character in patternEffect
_Matches any single character
%Matches any substring, including the empty string
<any other character> Matches only the exact same character

If either operand is NULL, the result of the LIKE operation is UNKNOWN.

To explicitly match a special character in the character string, you must specify an escape character using the ESCAPE clause. The escape character must then precede the special character in the pattern. See examples below.

OperationResult
'a' LIKE 'a'TRUE
'a' LIKE 'b' FALSE
'ab' LIKE 'a%' TRUE
'ab' LIKE 'a\%' ESCAPE '\' FALSE
'a' LIKE 'a%' TRUE
'abcd' LIKE 'a%' TRUE
'1a' LIKE '_a' TRUE
'123aXYZ' LIKE '_%_a%_' TRUE

SIMILAR TO patterns

SIMILAR TO compares a string to a pattern. It is much like the LIKE operator, but more powerful, as the patterns are regular expressions.

seq in the SIMILAR TO table below means any sequence of characters explicitly specified, such as ‘13aq’. Non-alphanumeric characters intended for matching must be preceded by an escape character explicitly declared in the SIMILAR TO statement, such as ‘13aq!’ SIMILAR TO ‘13aq!24br!% ESCAPE ‘\’ (This statement is TRUE).

When a range is indicated, as when a dash is used in a pattern, the curent collating sequence is used. Typical ranges are 0-9 and a-z. This link provides a typical discussion of pattern-matching, including ranges.

When a line requires multiple comparisons, the innermost pattern that can be matched will be matched first, then the “next-innermost,” etc.

Expressions and matching operations that are enclosed within parentheses are evaluated before surrounding operations are applied, again by innermost-first precedence.

SIMILAR TO Table

Delimiter Character in pattern Effect Rule
parentheses ( ) ( - seq ) Groups the - seq (used for defining precedence of pattern expressions) 1
brackets [ - seq ] Matches any single character in the seq 2
caret or
circumflex
[^seq] Matches any single character not in the seq 3
[seq^seq] Matches any single character in seq and not in the seq 4
dash - Specifies a range of characters between character1 and character2
(using some known sequence like 1-9 or a-z) 5
bar [seq seq] Matches either seq or seq 6
asterik seq* Matches zero or more repitition of seq 7
plus seq+ Matches one or more repetitions of seq 8
braces seq{} Matches exactly number of repetitions of seq 9
seq {,} Matches low number or more repetitions of - seq, to a maximum of high number 10
question - mark seq? Matches zero or one instances of seq 11
underscore _ Matches any single character 12
percent % Matches any substring, including the empty string 13
character Matches only the exact same character 14
NULL NULL If either operand is NULL, the result of the SIMILAR TO operation is UNKNOWN. 15
Non - alphanumeric Special Characters that special character must be preceded by an escape character defined using
an ESCAPE clause specified at the end of the pattern. 16

See examples below.

Operation Result Rule
‘a’ SIMILAR TO ‘a’ TRUE 14
‘a’ SIMILAR TO ‘b’ FALSE 14
‘ab’ SIMILAR TO ‘a%’ TRUE 13
‘a’ SIMILAR TO ‘a%’ TRUE 13
‘abcd’ SIMILAR TO ‘a%’ TRUE 13
‘1a’ SIMILAR TO ‘_a’ TRUE 12
‘123aXYZ’ SIMILAR TO ‘_%a% TRUE 13 & 12
‘abd’ SIMILAR TO ‘[ab][bcde]d’ TRUE 2
‘abd’ SIMILAR TO ‘[ab]d’ FALSE 2
‘cd’ SIMILAR TO ‘[a-e^c]d’ FALSE 4
‘yd’ SIMILAR TO ‘[^(a-e)]d’ INVALID
‘fred’ SIMILAR TO ‘amyfred’ TRUE 6
‘acd’ SIMILAR TO ‘ab*c+d’ TRUE 7 & 8
‘abd’ SIMILAR TO ‘ab*c+d’ FALSE 7 & 8
‘abb’ SIMILAR TO ‘a(b{3})’ FALSE 9
‘abbbbb’ SIMILAR TO ‘a(b{3})’ FALSE 9
‘abbbbbbbb’ SIMILAR TO ‘ab{3,6}’ FALSE 10
” SIMILAR TO ‘(ab)?’ TRUE 11
‘a’ SIMILAR TO ‘(ab)?’ FALSE 11
‘ab’ SIMILAR TO ‘ab?’ TRUE 11
‘abb’ SIMILAR TO ‘ab?’ FALSE 11
‘ab’ SIMILAR TO ‘a\%’ ESCAPE ‘\’ FALSE 16
‘a%’ SIMILAR TO ‘a\%’ ESCAPE ‘\’ TRUE 16
‘a(b{3})’ SIMILAR TO ‘a(b{3})’ ESCAPE ‘\’ TRUE 16