$FILTER in SAP OData

by | Jan 6, 2022 | ODATA

Home » SAP » ABAP » ODATA » $FILTER in SAP OData

Preface – This post is part of the SAP ABAP OData Tutorial series.

In this section, we will learn the concept of the $filter query in SAP OData and its usage.

Supported System Version

SAP NetWeaver Gateway Release 2.0 Support Package >=03

Syntax

http://<server>:<port>/sap/opu/odata/sap/<service_name>/EntitySet?$filter=ProductId eq ‘HP-101’

Business Example

The shopping site, say, Amazon is displaying all the products but you want some specific category to be displayed. So, you will apply filters (select category phones or laptops)  to display the data of your choice.

Implementation

In this section, we will implement $filter on our Product Entity Set.

Step 1: After the creation of the OData service, right Click on Entity Set under Service Implementation, go to ABAP workbench.

SAP OData Implementation

Step 2:

Adjust your code for $filter query.

$FILTER in SAP OData

The filter query option is available by accessing the parameter IT_FILTER_SELECT_OPTIONS.

In the above code, we first retrieved the filter options passed from the frontend by reading IT_FILTER_SELECT_OPTIONS. And then passed the select-option range to BAPI to get the desired filtered output.

Step 3:

If we do not pass any filter option, we will get all the products from the system.

Add $filter query in OData URL and execute the service. You can add one or more filter options.

Service URL: /sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$filter=Name eq ‘Notebook Basic 15.

 

Operators used with $filter

Logical Operators

Logical OperatorDescriptionExample
eqEqualhttps://<API-Server>/odata/v2/User?$filter=gender eq ‘F’ .
neNot Equalhhttps://<API-Server>/odata/v2/User?$filter=place ne ‘’London’
gtGreater thanhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount gt 200
geGreater than or equalhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount ge 200
ltLess thanhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount lt 200
leLess than or equalhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200
andLogical andhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 and amount ge 300
orLogical orhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 or amount ge 300
notLogical nothttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 not amount ge 300

 

Arithmetic Operators

Arithmetic OperatorDescriptionExample
addAdditionhttps://<API-Server>/odata/v2/PicklistLabel?$filter=amount add 5 gt 10
subSubtractionhttps://<API-Server>/odata/v2/PicklistLabel?$filter= amount sub 5 gt 10
mulMultiplicationhttps://<API-Server>/odata/v2/PicklistLabel?$filter= amount mul 5 gt 10
divDivisionhttps://<API-Server>/odata/v2/PicklistLabel?$filter= amount div 5 gt 10
modModulushttps://<API-Server>/odata/v2/PicklistLabel?$filter= amount mod 5 gt 10

 

Grouping Operators

Grouping OperatorDescriptionExample
( )Precedence groupinghttps://<API-Server>/odata/v2/PicklistLabel?$filter=(number sub 20) gt 30

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author