Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Operands and Expressions →  ABAP SQL - SQL Expressions sql_exp →  sql_exp - sql_case →  sql_exp - sql_simple_case 

SQL Expressions, Simple CASE

This example demonstrates simple case distinctions in SQL expressions.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DELETE FROM demo_expressions.
    INSERT demo_expressions FROM TABLE @( VALUE #(
      ( id = 'x' char1 = 'aaaaa' char2 = 'bbbbb' )
      ( id = 'y' char1 = 'xxxxx' char2 = 'yyyyy' )
      ( id = 'z' char1 = 'mmmmm' char2 = 'nnnnn' ) ) ).

    DATA(else) = 'fffff'.
    SELECT id, char1, char2,
           CASE char1
             WHEN 'aaaaa' THEN ( char1 && char2 )
             WHEN 'xxxxx' THEN ( char2 && char1 )
             ELSE @else
           END AS text
           FROM demo_expressions
           INTO TABLE @DATA(results).

    cl_demo_output=>display( results ).

Description

In a list of columns specified after SELECT, CASE is used to make a simple case distinction for the content of a column. The result is a character string chained from column content or taken from a host variable. Using the alias name defined after AS, the result is assigned to the column with the same name in an internal table declared inline, results.