Search code examples
abapopensql

Substring length as variable in OpenSQL query?


I want to select the length of the string ( LGOBE ) minus 4.

But i get this error: "In the function "SUBSTRING", the parameter number "3" must be an ABAP variable. This is not the case for the expression that starts with "LENGTH"."

What I understand from this message is that i cannot use a function as the third parameter, is this interpretation correct? If so, is there an alternative for me to remove the last 4 places of the string inside the select statement? Thank you.

  SELECT
  SUBSTRING( LGOBE,1, LENGTH( LGOBE ) - 4 ) AS TEST1,
  LGOBE, WERKS
  FROM T001L INTO TABLE @DATA(IT_FINAL)
  UP TO 100 ROWS.

  CL_DEMO_OUTPUT=>DISPLAY( IT_FINAL ).

Solution

  • Checking the 754 documentation:

    SUBSTRING( sql_exp,pos,len ) sql_exp: see below

    pos: Literal, host variable, or host expression with the ABAP type b, s, i, int8

    len: Literal or host constant with the ABAP type b, s, i, int8

    Now 755:

    SUBSTRING( sql_exp,pos,len )

    Substring sql_exp from the position pos with length len. pos and len must be specified so that the substring is within sql_exp. sql_exp: see below

    pos: SQL expression with the ABAP type b, s, i, int8

    len: SQL expression with the ABAP type b, s, i, int8

    Interestingly I cannot find that mentioned in the release notes itself. So either you upgrade to 755 release or you'll have to do the processing in ABAP after selection.