Enterprise Resource Planning Blogs by Members
Gain new perspectives and knowledge about enterprise resource planning in blog posts from community members. Share your own comments and ERP insights today!
cancel
Showing results for 
Search instead for 
Did you mean: 
SeshaP
Participant
Hi All,

This blog post provides alpha numeric conversion or handling leading zero’s at ABAP CDS view.

Introduction: This is a common requirement to convert alpha numeric or handling leading zeros for an object or a field in any report. In SAP for the standard fields like Material, purchase order, physical inventory document or any field by default we will have pre-defined data type with pre-defined length.

Example:

  • Material                 -- MARA.MATNR = 18

  • Purchase order     -- EKKO.EBELN =10

  • Inv Doc                  -- ISEG.IBLNR =10


If we don’t maintain or pass the length of characters, by default it will add leading 0’s to the value. Example, for a material in SAP at the time of creating a material we have manually created it with length 7  as “8057869” in this case it will store at the table as “000000000008057869”

To implement Alpha numeric conversion at CDS, we have to use function “ABAP_ALPHANUM”. This function can be readable through a class and a table function.

Below is the snippet code to implement alpha numeric conversion at ABAP CDS.

Class:



CLASS ZCL_ALPHANMC_TEST DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .


PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
CLASS-METHODS exec_method FOR TABLE FUNCTION ZT_ALPHANMC_TEST.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.


CLASS ZCLALPHANMC_TEST IMPLEMENTATION.
METHOD exec_method
BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
using ZTABLE.
/*ZTABLE is the custom table and ZMATNR is the field converting to alpha numeric*/
ittable = select mandt as client,
ABAP_ALPHANUM(ZMATNR,18) as Material
from ZTABLE;
/* ABAP_ALPHANUM is the function to convert or add leading zeros to a field, 18 is the Length depending on requirement we can change */
RETURN SELECT CLIENT,
Material
from it_table;
ENDMETHOD.
ENDCLASS.

Table function: Reading data from a class



@EndUserText.label: 'Alpha Numeric Conversion Test view'
define table function  ZT_ALPHANMC_TEST
returns
{
Client:abap.clnt;
Material:abap.char(18);
}
implemented by method ZCL_ALPHANMC_TEST=>exec_method;

Read the alpha numeric converted values in the final cds view using Table function.

More info on class & table functions is avilable at

https://help.sap.com/docs/SAP_NETWEAVER_AS_ABAP_752/f2e545608079437ab165c105649b89db/e5529f75afbc43e...

 

To conclude, this blog post can be helpful for the developers who are having  issues with leading zeros or would like to convert any field to alpha numeric conversion at abap cds view.

 

Hope this blog post helps.

 

Thanks & Regards,

Seshu
4 Comments
Labels in this area