讲解了ABAP SQL Functions for Strings的使用

14 篇文章 1 订阅

4.1 CONCAT( arg1,arg2 )

Concatenates strings in arg1 and arg2. Trailing blanks in arg1, arg2, and in the result are ignored. The maximum length of the result is 1333.

示例代码:

SELECT matnr,

        concat( ersda, created_at_time ) AS str,

        ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

4.2 CONCAT_WITH_SPACE( arg1, arg2, spaces )

Concatenates 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.

示例代码:

SELECT matnr,

       concat_with_space( ersda, created_at_time, 5 ) as str,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

其中的spaces是数字,代表了两个字段之间需要多少空格

Debug时数据如下图所示:

4.3 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.

示例代码:

SELECT matnr,

       instr( vpsta,'CV' ) AS num,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的作用就是在arg中查找sub,并返回位置,如果没有找到,则返回0。就这个意思

4.4 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.

示例代码:

SELECT matnr,

left( vpsta,3 ) AS num,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

字符串arg从左边开始,取前len长度的字符。

4.5 LENGTH( arg )

Number of characters in arg ignoring trailing blanks.

示例代码:

SELECT matnr,

       length( vpsta ) AS num,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

获取字段的长度,就这么简单

4.6 LOWER( arg )

String with a length of arg, in which all uppercase letters are transformed to lowercase letters.

示例代码:

SELECT matnr,

lower( vpsta ) as str,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的功能就是将字段的值转换为小写

4.7 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). Leading 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.

示例代码:

SELECT matnr,

       lpad( vpsta, 20, '123' ) AS str,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的意思是:len指定字段的长度,arg是内容,如果内容的长度不够,则在左边用src填充到len指定长度为止。

4.8 LTRIM( arg, char )

String with the content of arg in which all trailing blanks and leading characters are removed that match the character in char. A blank in char is significant.

示例代码:

SELECT matnr,

       ltrim( ersda,'2' ) AS str,

       ernam

FROM mara

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

可以看到日期的年2020最前面的2被移除了。

LTRIM的意思就是移除字符串右侧的空白字符(trailing blanks尾随空格)和左侧第一个匹配的指定的字符(大概就是这意思)

4.9 REPLACE( arg1, arg2, arg3 )

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.

示例代码:

SELECT matnr,

replace( vpsta,'VE','HU' ) AS str,

       vpsta,

       ernam

FROM mara

WHERE vpsta LIKE 'KCV%'

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的意思就是在字段arg1中查找arg2,如果找到,然后将arg2的值替换成arg3。

4.10 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.

示例代码:

SELECT matnr,

     right( matkl,3 ) AS str,

     matkl

FROM mara

WHERE vpsta LIKE 'KCV%'

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

从右往左截取固定长度

其实该语法也可以用于inner join

SELECT a~*

FROM vbrp AS a

  INNER JOIN ekpo AS b ON a~aubel = b~ebeln

AND right( a~aupos,5 ) = b~ebelp

WHERE b~ebeln <> ''

INTO TABLE @DATA(gt_out).

从而可以解决在inner join时字段长度不一致的问题。

4.11 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). Leading 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.

示例代码:

SELECT matnr,

     rpad( matkl,10,'abc' ) AS str,

     matkl

FROM mara

WHERE vpsta LIKE 'KCV%'

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的用法就是取字段arg,然后用src补足长度len。

4.12 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.

示例代码:

SELECT matnr,

     rtrim( matkl,'1' ) AS str,

     matkl

FROM mara

WHERE vpsta LIKE 'KCV%'

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数功能是在arg的右边查找char字符,如果找到,则去掉;

4.13 SUBSTRING( arg, pos, len )

Substring arg from the position pos with length len. pos and len must be specified so that the substring is within arg.

示例代码:

SELECT matnr,

     substring( matkl,3,2 ) AS str,

     matkl

FROM mara

WHERE vpsta LIKE 'KCV%'

INTO TABLE @DATA(gt_out)  UP TO 10 ROWS.

Debug时数据如下图所示:

该函数的功能就是:取字段arg,然后从pos位开始(从1开始数),截取长度len的字符串;

4.14 UPPER( arg )

String with a length of arg, in which all lowercase letters were transformed to uppercase letters.

这个功能太简单了,我就懒得写了。

创建by hujie 2020-12-22 15:20:05

  1. COALESCE( sql_exp1, sql_exp2, ..., sql_expn )

不知道怎么翻译该函数的名称,就叫凝聚函数吧。该函数的功能是将参数中的第一个不为空的参数返回来。

举个例子:

SELECT

FROM mara

FIELDS matnr,

         coalesce(  CASE vpsta WHEN 'KDG' THEN 'HUJIE' END,

CASE vpsta WHEN 'KDA' THEN 'LAIMIN'

ELSE 'DUODUO'

END

) AS str,

        vpsta

WHERE vpsta LIKE 'KD%'

INTO TABLE @DATA(lt_out) UP TO 10 ROWS.

Debug时结果如下图所示:

可能有点玄幻,那再举一个例子:

SELECT LFA1~LIFNR,

COALESCE( EKKO~LIFNR, EKKO~EBELN, '无采购订单' ) as vendor

FROM LFA1

INNER JOIN EKKO ON LFA1~LIFNR = EKKO~LIFNR

INTO TABLE @DATA(LT_OUT).

  1. UUID(  )

每条数据可产生UUID类型的数据

SELECT matnr,

        vpsta,

       uuid( ) AS uuid

FROM mara

WHERE vpsta LIKE 'KD%'

INTO TABLE @DATA(lt_out) UP TO 10 ROWS.

  1. WITH

该语法的主要作用是先读取一个表当做子查询,然后使用inner join关联到主表中,用例子说明吧,如下图所示:

WITH +ztmm0030 AS (

SELECT spart,

           matkl

FROM ztmm0030

WHERE spart = 'Y1'

)

SELECT a~matnr,

         a~matkl,

         b~maktx

FROM mara AS a

         INNER JOIN makt AS b ON a~matnr = b~matnr

         INNER JOIN +ztmm0030 AS c ON a~matkl = c~matkl

INTO TABLE @DATA(lt_out).

SORT lt_out BY matkl.

IF sy-subrc = 0.

WRITE: / 'Yes'.

ELSE.

WRITE: / 'No'.

ENDIF.

该sql首先取ztmm0030表数据,查询条件是该表的spart = 'Y1',给该查询的结果取个名字,必须以+开头,我就取为+ztmm0030,下面的主查询使用inner join将+ztmm0030关联到其他表。作用就是缩小一些范围来查询数据呗,同时也可以将+ztmm0030的字段取出。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值