SQL Functions

«« Previous
Next »»

The following sections summarize the predefined SQL functions supported by ABAP CDS and Open SQL, regardless of platform.

◉ SQL functions for numeric values


The following table shows the numeric SQL functions 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
ABS(arg) Absolute amount of arg. x x
CEIL(arg) Smallest integer number not less than the value of arg.
DIV(arg1, arg2) Integer part of the division of arg1 by arg2 The sign is assigned after the amounts are divided; positive if the arguments have the same sign, and negative if the arguments have different signs. Exception: arg2 has the value 0.
DIVISION(arg1, arg2, dec) Division of arg1 by arg2 The result is rounded to dec decimal places.
FLOOR(arg) Largest integer number not greater than the value of arg.
MOD(arg1, arg2) Positive or negative integer remainder of the division of arg1 by arg2.
ROUND(arg, pos) Rounded value of arg. If pos is greater than 0, the value is rounded to the position pos on the right of the decimal separator. If this is not the case, position abs(pos)+1 to the left of the decimal separator is rounded. This results in a 0 if the number of places is not sufficient.

Descriptions of the potential operands and data types can be found the corresponding documentation for ABAP CDS and Open SQL.

Note

The SQL functions DIV and MOD behave differently with respect to the signs than the ABAP operators DIV and MOD. In the SQL function DIV, the amounts of the arguments are divided and then the sign is assigned (positive if the arguments have the same signs and negative if they have different signs). Accordingly, the result of MOD can be negative, so that multiplying the result of DIV with expr2 plus the result of MOD produces the value of expr1. The ABAP operator MOD, on the other hand, only produces positive results.

Example

The following table shows the results of integer divisions and their integer remainders in SQL. See also the example for the ABAP operators DIV and MOD.

expr1 expr2 DIV MOD
7 3 2 1
-7 3 -2 -1
7 -3 -2 1
-7  -3 -1 

◉ SQL functions for strings


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. -
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. -
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. -
LENGTH(arg) Number of characters in arg ignoring trailing blanks.
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.
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.
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.
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.
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.
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

◉ SQL functions for byte strings


SQL Function Result ABAP CDS Open SQL
BINTOHEX(arg) Character string containing the half bytes arg converted to the hexadecimal characters "0" to "9" and "A" to "F" (left-justified). x -
HEXTOBIN(arg) Byte string whose half bytes are determined from the hexadecimal characters in arg. Any leading blanks are removed before the conversion from arg and all trailing blanks are then replaced by "0". -

«« Previous
Next »»

No comments:

Post a Comment