# Relational operators in SAP ABAP

Last Updated : 22 Dec, 2023

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

## 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'. " TrueELSE.  result = ' '. " FalseENDIF.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`

## FAQs On Relational Operator in SAP ABAP

### 1. What is Between Operator in SAP ABAP?

Answer: Between Operator in SAP ABAP is a relational operator, it returns true if a lies in between b andÂ c.

### 2. What are logical Operator in SAP ABAP?

Answer: Logical Operators are used in SAP ABAP for comparing the data. these are also known as conditional or relational operators. These operators include equal to (EQ), not equal toÂ (NE),Â less than (LT), less than or equal to(LE)Â , greater than(GT)Â , and greater than or equal toÂ (GE).Â Comparison operators in SAP ABAP are utilized to compare data and offer conclusions depending on the comparisonâ€™s results.Â Please refer the above mentioned table for more details.

### 3. What are Reduce Operator in SAP ABAP?

Answer: Reduce Operator in SAP ABAP is used to reduce a list of objects. it is generally used to reduce sets of data objects to a single data objects.

### 4. What are Ternary Operator in SAP ABAP?

Answer: There is no any Ternary Operator in SAP ABAP , but we have an alternative for doing this operation. by using the below syntax we can achieve this operation:

`COND type(Â [let_exp]Â Â Â Â Â Â Â Â Â Â Â Â Â Â  WHENÂ log_exp1Â THENÂ [Â let_exp]Â result1Â Â Â Â Â Â Â Â Â Â Â Â Â [Â WHENÂ log_exp2Â THENÂ [Â let_exp]Â result2Â ]Â Â Â Â Â Â Â Â Â Â Â Â  ...Â Â Â Â  Â Â Â Â Â Â Â Â [Â ELSEÂ [Â let_exp]Â resultnÂ ]Â ) ...`

These relational operators are used in SAP ABAP within conditional statements (e.g., `IF`, `CASE`, etc.) to control the flow of the program based on the comparison results.

Previous
Next