ABAP CDS 函数

版权声明:如有转载请注明 by Huang.Lin https://blog.csdn.net/huanglin6/article/details/82627757

The following table shows the potential SQL functions for strings in a CDS view in ABAP CDS, plus the requirements made on the arguments.. The meaning of the functions can be found under SQL Functions for Strings.

Function Valid Argument Types Result Type
CONCAT(arg1, arg2) See below SSTRING if an argument has the type SSTRING, else CHAR with the length of the result.
CONCAT_WITH_SPACE(arg1, arg2, spaces ) arg1, arg2: see below 

spaces: positive numeric literal greater than 0 and less than or equal to 1331
SSTRING if an argument has the type SSTRING, else CHAR with the length of the result.
INSTR(arg, sub) arg: see below 

sub: non-empty numeric literal
INT4
LEFT(arg, len) arg: see below 

len: positive numeric literal greater than 0 and less than or equal to 1333
SSTRING if arg has the type SSTRING, else CHAR with length len
LENGTH(arg) See below INT4
LPAD(arg, len, src) arg: see below 

len: positive numeric literal greater than 0 and less than or equal to 1333 

src: character Literal
SSTRING if arg has the type SSTRING, else CHAR with length len
LTRIM(arg, char) arg: see below 

char: Character literal with length 1
SSTRING if arg has the type SSTRING, else CHAR with the length of arg.
REPLACE(arg1, arg2, arg3) See below SSTRING if arg1 or arg3 has the type SSTRING, else CHAR with the maximum possible length of the result.
RIGHT(arg,len) arg: see below 

len: positive numeric literal greater than 0 and less than or equal to 1333
SSTRING if arg has the type SSTRING, else CHAR with length len
RPAD(arg, len, src) arg: see below 

len: positive numeric literal greater than 0 and less than or equal to 1333 

src: character literal
SSTRING if arg has the type SSTRING, else CHAR with length len
RTRIM(arg, char) arg: see below 

char: Character literal with length 1
SSTRING if arg has the type SSTRING, else CHAR with the length of arg.
SUBSTRING(arg, pos, len) arg: see below 

pos and len: positive numeric literal not equal to zero
SSTRING, if arg has the type SSTRING, else CHAR or NUMC with a length of at least len

The following table shows the SQL functions for strings supported by ABAP CDS and Open SQL. The last two columns indicate where a function can be used.

SQL Function Result ABAP CDS Open SQL
CONCAT(arg1, arg2) Chaining of character strings in arg1 and arg2. Trailing blanks in arg1, arg2, and in the result are ignored. The maximum length of the result is 1333. x x
CONCAT_WITH_SPACE(arg1, arg2, spaces ) Concatenation of strings in arg1 and arg2 as with CONCAT. The number of blanks specified in spaces is inserted between arg1 and arg2. The maximum length of the result is 1333. x -
INSTR(arg, sub) Position of the first occurrence of the string from sub in arg (case-sensitive). arg respects leading blanks and ignores trailing blanks. sub respects all blanks. sub must contain at least one character. If no occurrences are found, the result is 0. x -
LEFT(arg, len) String of the length len with the len left characters of arg (ignoring the trailing blanks). The value of len cannot be greater than the length of arg. x -
LENGTH(arg) Number of characters in arg ignoring trailing blanks. x x
LPAD(arg, len, src) String of the length len with the right-justified content of arg without trailing blanks and in which leading blanks produced by the expanded string are replaced by the characters from the argument src (respecting all blanks). Trailing blanks from arg are preserved. If more characters are required than exist in src, the content of src is used repeatedly. If len is less than the length of arg, it is truncated on the right. If src is empty and len is greater than the length of arg, arg remains unchanged. x x
LTRIM(arg, char) String with the content of arg in which all trailing blanks are removed and all leading characters that match the character in char. A blank in char is significant. x x
REPLACE(arg1, arg2, arg3) Character string arg1, in which all instances of arg2 are replaced by the content from arg3. The replacement of letters is case-sensitive. Trailing blanks are ignored in all arguments. The maximum length of the result is 1333. x x
RIGHT( arg, len ) String of the length len with the len right characters of arg (ignoring the trailing blanks). The value of len cannot be greater than the length of arg. x x
RPAD(arg, len, src) String of the length len with the left-justified content of arg without trailing blanks and in which trailing blanks produced by the expanded string are replaced by the characters from the argument src (respecting all blanks). Trailing blanks from arg are preserved. If more characters are required than exist in src, the content of src is used repeatedly. If len is less than the length of arg, it is truncated on the right. If src is empty and len is greater than the length of arg, arg remains unchanged. x -
RTRIM(arg, char) String with the content of arg in which all trailing blanks are removed and all trailing characters that match the character in char. A blank in char is significant. x x
SUBSTRING(arg, pos, len) Substring of arg from the position pos in the length len. pos and len must be specified so that the substring is within in arg. x x

The following can be specified as the arguments arg:

  • Literals of a suitable type. The literal can be prefixed with the name of a domain.
  • The following predefined functions and expressions (if they return a matching type):
  • Type modifications using CAST

The valid argument types for arg, arg1, arg2, and arg3 are CHARCLNTLANGNUMCCUKYUNITDATSTIMS, and SSTRING.

In functions where an explicit length len is specified, the actual length of the result is defined when the CDS view is activated and is at least as long as len.

In all functions with the exception of LPAD and RPAD, the trailing blanks of all arguments are removed before the actual processing and the trailing blanks of the result are removed before the return operation. In LPAD and RPAD, the trailing blanks of the argument src are preserved.

Note

The characters in the surrogate area of the system code page UTF-16 are handled as two characters by the CDS string functions. This must be respected when the length is determined and these characters must not be split by mistake.

Example

The following CDS view applies predefined SQL functions for strings in the SELECT list to columns of the database table DEMO_EXPRESSIONS. The program DEMO_CDS_SQL_FUNCTIONS_STRING uses SELECT to access the view.

@AbapCatalog.sqlViewName: 'DEMO_CDS_STRFUNC' 
@AccessControl.authorizationCheck: #NOT_REQUIRED 
  define view demo_cds_sql_functions_string 
   as select from demo_expressions 
   { length(            char1               ) as r_length, 
     instr(             char1, 'CD'         ) as r_instr, 
     concat(            char1, char2        ) as r_concat, 
     concat_with_space( char1, char2, 10    ) as r_concat_with_space, 
     left(              char1, 3            ) as r_left, 
     right(             char2, 3            ) as r_right, 
     lpad(              char1, 10, 'x'      ) as r_lpad, 
     rpad(              char2, 10, 'y'      ) as r_rpad, 
     ltrim(             char1, 'A'          ) as r_ltrim, 
     rtrim(             char1, 'E'          ) as r_rtrim, 
     replace(           char2, 'GHI', 'XXX' ) as r_replace, 
     substring(         char2, 2, 3         ) as r_substring }

猜你喜欢

转载自blog.csdn.net/huanglin6/article/details/82627757