Skip to Content
Technical Articles

Startswith, Endswith query options in OData

Description: The blog post is about to understand the startswith and endswith filter concept in OData framework.

Prerequisite: To know the behaviour of startswith and endswith in OData framework before we
needs to know about  the GET_ENTITYSET method and parameters.

Introduction: $filter query option is the most used query option while accessing OData service.The
framework does not provide default filtering.We need to implement it ourself.$filter query option
allows us to filter the result set of GET_ENTITYSET implementation.

we are having multiple filter options such as :

  • startswith
  • endswith
  • substringof
  • count
  • exclusive

Here in this blog i will be explaining you about startswith and endswith,

startswith: If we want to fetch data based on a particular starting value we can use startswith.

Below is the example for fetching data based on ‘CARRID’ startswithA’ from ‘SFLIGHT’ database table.

Redefine GET_ENTITYSET methiod in DPC_EXT class and write the below logic,

method SFLIGHTSET_GET_ENTITYSET.

DATA: LT TYPE ZCL_ZBP_FLIGHT_MPC_EXT=>TT_SFLIGHT.

READ TABLE IT_FILTER_SELECT_OPTIONS INTO DATA(LS_FILTER)

WITH KEY PROPERTY = ‘Carrid’.

LOOP AT LS_FILTER-SELECT_OPTIONS INTO DATA(LS_FILTER_SOPTION).

IF  LS_FILTER_SOPTION IS NOT INITIAL.

REPLACE ALL OCCURRENCES OF ‘*’ IN LS_FILTER_SOPTION-LOW WITH ‘%’.

SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE LT
WHERE PLANETYPE LIKE LS_FILTER_SOPTION-LOW.

IF SY-SUBRC = 0.

ET_ENTITYSET = LT.

ENDIF.

ENDIF.

ENDLOOP.

endmethod.

 

Now we can test our $filter startswith query option in gateway client by using below URL.

URL:

/sap/opu/odata/SAP/ZBP_FLIGHT_SRV/sflightSet?$filter=startswith(Carrid,’A’)

Image%20source%20%3ADamodar%20%2CSAP%20ABAP%20technical%20consultant

Image source :Damodar ,SAP ABAP technical consultant

Fig1:- You can see in the above picture the records are fetched from the database with starting value ‘A’.

 

Endswith:- If we want to fetch data based on a particular ending value we can use Ends with.

Below is the example for fetching data based on ‘CARRID’ endswith ‘L’ from ‘SFLIGHT’ database table.

Redefine GET_ENTITYSET methiod in DPC_EXT class and write the below logic,

method SFLIGHTSET_GET_ENTITYSET.

DATA: LT TYPE ZCL_ZBP_FLIGHT_MPC_EXT=>TT_SFLIGHT.

READ TABLE IT_FILTER_SELECT_OPTIONS INTO DATA(LS_FILTER)

WITH KEY PROPERTY = ‘Carrid’.

LOOP AT LS_FILTER-SELECT_OPTIONS INTO DATA(LS_FILTER_SOPTION).

IF  LS_FILTER_SOPTION IS NOT INITIAL.

REPLACE ALL OCCURRENCES OF ‘*’ IN LS_FILTER_SOPTION-LOW WITH ‘%’.

SELECT * FROM SFLIGHT INTO CORRESPONDING FIELDS OF TABLE LT
WHERE PLANETYPE LIKE LS_FILTER_SOPTION-LOW.

IF SY-SUBRC = 0.

ET_ENTITYSET = LT.

ENDIF.

ENDIF.

ENDLOOP.

endmethod.

 

Now we can test our $filter endswith query option in gateway client by using below URL.

URL:  /sap/opu/odata/SAP/ZBP_FLIGHT_SRV/sflightSet?$filter=endswith(Carrid,’L’)

 

Image%20source%3A%20Damodar%2C%20SAP%20ABAP%20technical%20consultant

Image source: Damodar, SAP ABAP technical consultant

Fig2:- You can see in the above picture the records are fetched from the database with ending value ‘L’.

Conclusion: This is how startswith and endswith works in ODATA. Hope this document will help for those who are new to ODATA.

 

Thank You 🙂

1 Comment
You must be Logged on to comment or reply to a post.