Open In App

Relational operators in SAP ABAP

Last Updated : 22 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the program's logic. Relational operators in SAP ABAP are employed in conditional statements and expressions to control the flow of a program. They help determine whether a particular condition is true or false, guiding the program to execute specific code blocks accordingly. The relational operators in SAP ABAP include equality, inequality, greater than, less than, greater than or equal to, and less than or equal to.


Relational-Operators-in-SAP-ABAP-12
Relational operators in SAP ABAP

What are Conditional/ Relational Operators in SAP ABAP?

Comparison operators in SAP ABAP are utilized to compare data and offer conclusions depending on the comparison’s results. These operators include equal to (EQ), not equal to (NE), less than (LT), less than or equal to(LT) , greater than(GT) , and greater than or equal to (GE). Here we are sharing you a table in which every comparison operator is discussed in detail.

Relational Operator for All data types in SAP ABAP

Operator

Function

=, EQ

returns true If two operands are equal to each other

<>, NE

returns true If two operands are not equal to each other

<, LT

returns true If first operand is less than second.

>, GT

returns true If first operand is greater than second.

<=, LE

returns true If first operand is less than or equal to second.

>=, GE

returns true If first operand is greater than or equal to second.

IS INITIAL

returns true if the variable declared is not modified

IS NOT INITIAL

returns true if the variable declared is modified

a BETWEEN b AND c (INTERVAL TEST)

returns true if a lies in between b and c

Note: The above relational operators works for all data types in SAP ABAP.

Here’s an example:

DATA: value1 TYPE I VALUE 10,
value2 TYPE I VALUE 20,
result TYPE C LENGTH 1.
IF value1 EQ value2.
result = 'X'. " True
ELSE.
result = ' '. " False
ENDIF.4


Output:

False


Relational operator for char data types in SAP ABAP:

Here we are sharing you a table in which every Character string relational operator is explained:

Operator

Function

CO (Contains Only)

returns true if A consists of only the characters in B.

CN (Not Contains ONLY)

returns true if A consists contains at least one character not present in B.

CA (Contains ANY)

returns true if A contains at least one character of B.

NA (NOT Contains Any)

 returns true if A does not contain any character present in B

CS (Contains a String)

returns true if A contains the entire string B

NS (NOT Contains a String)

returns  true if A does not contains the entire string B

CP (Contains a Pattern)

returns true if A contains the pattern in B

NP (NOT Contains a Pattern)

returns true if A does not contain the pattern in B

Example:

. DATA: P(10) TYPE C VALUE 'GFG',
Q(10) TYPE C VALUE 'GEEKS'.
IF P CA Q.
WRITE: / 'P contains at least one character of Q'.
ENDIF.


Output:

P contains at least one character of Q


Examples of Relational Operators in SAP ABAP:

Let's delve into each relational operator of SAP ABAP with examples:

Equals (=) Operator in SAP ABAP:

The Equals(=) operator in SAP ABAP Checks if two values are equal .

DATA(lv_variable1) TYPE i VALUE 5.
DATA(lv_variable2) TYPE i VALUE 5.

IF lv_variable1 = lv_variable2.
WRITE: 'Both variables are equal.'.
ENDIF.


Output:

Both variables are equal


Not Equals (<>) Operator in SAP ABAP:

The Not Equals(<>) operator in SAP ABAP Checks if two values are not equal to each other.

DATA(lv_variable1) TYPE i VALUE 5.
DATA(lv_variable2) TYPE i VALUE 10.

IF lv_variable1 <> lv_variable2.
WRITE: 'Both variables are not equal.'.
ENDIF.


Output:

Both variables are not equal


Less Than (<) Operator in SAP ABAP:

The less than (<) Operator in SAP ABAP Checks if the left operand is less than the right operand.

DATA(lv_variable1) TYPE i VALUE 5.
DATA(lv_variable2) TYPE i VALUE 10.

IF lv_variable1 < lv_variable2.
WRITE: 'Variable1 is less than Variable2.'.
ENDIF.


Output:

Variable1 is less than Variable2.


Greater Than (>) Operator in SAP ABAP:

The Greater than (>) Operator in SAP ABAP Checks if the left operand is greater than the right operand.

DATA(lv_variable1) TYPE i VALUE 15.
DATA(lv_variable2) TYPE i VALUE 10.

IF lv_variable1 > lv_variable2.
WRITE: 'Variable1 is greater than Variable2.'.
ENDIF.


Output:

Variable1 is greater than Variable2


Less Than or Equal To (<=) Operator in SAP ABAP:

The Less than or Equal to (<=) Operator in SAP ABAP Checks if the left operand is less than or equal to the right operand.

DATA(lv_variable1) TYPE i VALUE 10.
DATA(lv_variable2) TYPE i VALUE 10.

IF lv_variable1 <= lv_variable2.
WRITE: 'Variable1 is less than or equal to Variable2.'.
ENDIF.


Output:

Variable1 is less than or equal to Variable2


Greater Than or Equal To (>=) Operator in SAP ABAP:

The Greater than oe equal to (>=) Operator in SAP ABAP Checks if the left operand is greater than or equal to the right operand.

DATA(lv_variable1) TYPE i VALUE 10.
DATA(lv_variable2) TYPE i VALUE 5.

IF lv_variable1 >= lv_variable2.
WRITE: 'Variable1 is greater than or equal to Variable2.'.
ENDIF.


Output:

Variable1 is greater than or equal to Variable2



Next Article
Article Tags :

Similar Reads