CDS

CAST in CDS

CAST is used to change the data types in CDS views. Data type changes are needed for arithmetic calculation, for comparison during select or just to change the display option for that field. It is important to note here that there are some restrictions (more info here) on Casting one data type to another. Or can you?

Let’s jump to an example. In this example, we are converting a quantity field to a floating-point in order to perform division (done in higher level CDS).

define view ZCDS_CAST_EKKO as select from ekko 
inner join ekpo
on ekko.ebeln = ekpo.ebeln
inner join eket
on ekpo.ebeln = eket.ebeln
and  ekpo.ebelp = eket.ebelp {
    
    key ekko.ebeln,
    key ekpo.ebelp,
    key eket.etenr,
    
        ekpo.matnr,
        ekpo.werks,
        ekpo.lgort,
        ekpo.meins,
        ekpo.lmein,

        cast(ekpo.umren as abap.fltp) as umren,
        cast(ekpo.umrez as abap.fltp) as umrez,
        cast(eket.menge as abap.fltp) as menge,
        cast(eket.wemng as abap.fltp) as wemng

}

CAST can also directly choose a data element for conversion. In the following example, we are converting a 6 digit NUMC field to a character type of length 10. Comparing NUMC to NUMC is hard but comparing CHAR to CHAR is super easy.

define view zcds_casting_lips as select from lips {
    vbeln as Delivery,
    cast(posnr as char10) as item
      
}

Join our Linkedin page to get the latest updates.

error

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter
LinkedIn
LinkedIn
Share