ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Operands and Expressions → ABAP SQL - SQL Conditions sql_cond → sql_cond - rel_exp for Statements
sql_cond - LIKE
... operand1 [NOT] LIKE operand2 [ESCAPE esc] ...
This relational expression is true if the value of the operand
operand1 matches (does not match) the pattern in the operand
- The following applies to
- The data type of can be one of the dictionary types CHAR, NUMC, CLNT, LANG, DATS, TIMS, ACCP, CUKY, UNIT, or SSTRING.
- The following applies to
- Any trailing blanks are ignored by the pattern, even in the data type
- The content can be a maximum of twice the number of characters in the length of
operand1, but no more than 1333 characters. Character literals and constants, however, respect any trailing blanks and a syntax error occurs if this rule is broken. All other data objects ignore any trailing blanks and a runtime error occurs if this rule is broken.
- The content must match the data type of
operand1in accordance with the rules for lossless assignments. This is checked by the strict modes of the syntax check from Release 7.40, SP08 and can raise an exception .
A pattern in
operand2 is defined using the following wildcard characters:
- "%" is any character string (including an empty string)
- "_” stands for any character.
The pattern is case-sensitive. If the pattern in
operand2 consists of precisely
one "%" character, the evaluation of the condition in the database interface is optimized so that the
condition is not passed to the database and that, instead,
operand1 LIKE '%'
is always true regardless of the content of
operand1 NOT LIKE '%' is always false.
- The wildcard characters "_" and "%" are used as in standard SQL. Elsewhere in ABAP, the wildcard characters "+" and "*" are used in similar relational expressions, in particular when ranges table are used.
- It is not usually practical to define patterns in
operand2without wildcard characters. Instead, it is best to perform a comparison using
- Do not use patterns that are closed by wildcard characters to search for trailing blanks. The semantics of searches of this type are dependent on the database system that is used and in general do not produce the required result.
- Due to ABAP SQL-specific optimization,
operand1 LIKE '%'is also true if the operand
operand1contains null values.
- The trailing blanks in
operand2can also be ignored for the data type
string, which is a different property from all other operand positions in ABAP.
- The special character-like types
tcannot be used on the right side, since the wildcard characters "_" and "%" required for patterns are invalid content for these types.
- The maximum number of characters on the right side is twice the maximum in
operand1because, in theory, each character can be prefixed with an escape character defined using
Full text search in a table with text columns.
DATA srch_str TYPE string. cl_demo_input=>request( CHANGING field = srch_str ). IF srch_str IS INITIAL. RETURN. ENDIF. srch_str = '%' && srch_str && '%'. SELECT * FROM doktl WHERE id = 'SD' AND object LIKE 'AB%' AND langu = @sy-langu AND typ = 'E' AND doktext LIKE @srch_str INTO TABLE @DATA(text_tab) UP TO 100 ROWS. IF sy-subrc = 0. cl_demo_output=>display( text_tab ). ENDIF.
Pattern synchronization for a numeric column of the table DEMO_EXPRESSIONS.
CAST expression is used here to create the required character-like data type.
DELETE FROM demo_expressions. INSERT demo_expressions FROM TABLE @( VALUE #( ( id = 'X' num1 = 111 ) ( id = 'Y' num1 = 222 ) ( id = 'Z' num1 = 333 ) ) ). SELECT FROM demo_expressions FIELDS id, num1 WHERE CAST( num1 AS CHAR ) LIKE '2%' INTO TABLE @DATA(result). cl_demo_output=>display( result ).
... ESCAPE esc.
ESCAPE can be used to define a single-character
esc expects a
character-like data object with the length 1 containing the escape character. A
literal or a
host variable can be specified. In the pattern
operand2, the escape character in
esc may only
be placed before a wildcard character or before the escape character itself. In this case, these lose their special meaning.
If an escape character in
operand2 is not placed in front of a valid character, an exception of the class CX_SY_OPEN_SQL_DB is raised.
- The character # is recommended for the escape character
- One of the wildcard characters "_" and "%" can be specified as an escape character
esc, but is not recommended for reasons of readability.
- If the pattern in
operand2is created dynamically or comes from outside of the program, care must be taken that any escape characters it contains are not placed in front of characters other than "_", "%", or the escape character itself. To prevent an exception, escape characters that are part of the patter must be escaped using themselves before being used.
To search for the pattern '100%', the following expression can be used with
# as the escape character.
... LIKE '100#%' ESCAPE '#' ...