ABAP CDS 函数

9 篇文章 0 订阅

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.

FunctionValid Argument TypesResult Type
CONCAT(arg1, arg2)See belowSSTRING 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 belowINT4
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 belowSSTRING 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 FunctionResultABAP CDSOpen 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.xx
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.xx
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.xx
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.xx
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.xx
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.xx
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.xx
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.xx

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.
  • Suitable fields of a data source data_source of the current CDS view.
  • Path expressions that identify a suitable field of a data source data_source.
  • Input parameters from the parameter list parameter_list.
  • The following predefined functions and expressions (if they return a matching type):
  • Other predefined SQL functions
  • Arithmetic expressions
  • Type modifications using CAST

The valid argument types for arg, arg1, arg2, and arg3 are CHAR, CLNT, LANG, NUMC, CUKY, UNIT, DATS, TIMS, 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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joker-Full-stack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值