Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - WHERE →  WHERE - sql_cond 

sql_cond - LIKE

Other versions: 7.31 | 7.40 | 7.54

Syntax


... col [NOT] LIKE dobj [ESCAPE esc] ... 

Addition

... ESCAPE esc

Effect

This expression is true if the value of the column col fits (does not fit) the pattern in the host variable or the literal dobj. It is not possible to specify a column ID for dobj. The data types of the column col and the data object dobj must be character-like. The name of a host variable should be prefixed with the escape character @. The content of dobj should match the data type of the column in 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.

Wildcard characters can be used to create the pattern in dobj, where "%" represents any character string, even an empty one, and "_" represents any character. It is case-sensitive. Trailing blanks in dobj are ignored. This is also valid in particular for data objects of the type string whose trailing blanks are otherwise respected in ABAP.

If the pattern in dobj consists of precisely one "%" character, the WHERE condition in the database interface is optimized so that the condition is not passed to the database and that, instead, col LIKE '%' is always true regardless of the content of col and col NOT LIKE '%' is always false.


Notes

  • The use of the wildcard characters "_" and "%" corresponds to the standard of SQL. Elsewhere in ABAP, the wildcard characters "+" and "*" are used in similar relational expressions, in particular when selection tables are used.

  • 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 desired result.

  • Due to the Open SQL-specific optimization, col LIKE '%' is also true if the column col contains null values.

  • If col LIKE '%' is used in the ON condition of a join, nothing is optimized and the condition is passed to the database instead.

  • Host variables without the escape character @ are obsolete. The escape character @ must be specified in the strict modes of the syntax check from Release 7.40, SP05.

Example

Full-text search in a text table.

PARAMETERS srch_str TYPE c LENGTH 20. 

srch_str = '%' && srch_str && '%'. 

SELECT * 
       FROM doktl 
       WHERE doktext LIKE @srch_str 
       INTO TABLE @DATA(text_tab). 

Addition

... ESCAPE esc.

Effect

The addition ESCAPE can be used to define an escape character. esc must be a flat character-like data object with length one, whose content is used as an escape character. esc is always accessed like a data object of the data type c of the length 1. An escape character 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 is not placed in front of a valid character, an exception of the class CX_SY_OPEN_SQL_DB is raised. The addition ESCAPE cannot be used when reading pooled tables.


Notes

  • 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 dobj is 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.

Example

To search for the pattern '100%', the following expression can be used with # as the escape character.

... LIKE '100#%' ESCAPE '#' ...