Technical Articles
How to implement Alpha Numeric Conversion in ABAP CDS View
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
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
do you really need so much effort for this?
it is handled automatically.....
there are so many standard application based on cds, did you see any of those using table function for alpha conversion?
Hi Jun Wu,
Thank you for reading the blog and providing your feedback.
It depends on the requirement. I had a requirement to join Workflow Runtime table swwwihead with Physical Inventory document table ISEG.
Here I had to join Workflow table WI_TEXT field to Physical inv doc table IBLNR. They both are having different
Since datatype and predefined length are different from both the fields in this case, I had a leading zeros issue and to avoid I used alpha numeric conversion.
Note: if we join or map a field from standard SAP table to a standard table with same data type & predefined length then we don’t need any conversion Ex: MARC.MATNR =MARA.MATNR
https://stackoverflow.com/questions/65668980/add-leading-zeros-only-to-numeric-field-in-cds
If you have any other alternative solutions without using ALPHA numeric conversion, please let me know.
Thank you
you can cast WI_TEXT to 10 char field in one cds, then you join it with iblnr
I tried it didn't work . I tried with functions cast 10 and substring for converting text to char. If we join the custom field to an SAP doc table it doesn’t fetch all the values that matches with the join conditions.
Thank you