SAP ABAP CDS View – CAST (Type Conversion)

The CAST function in ABAP Core Data Services (CDS) Views is used to convert data types, ensuring compatibility or formatting changes within queries.


🔹 Basic Syntax of CAST in CDS

define view ZDEMO_CDS as select from sflight {
key carrid,
key connid,
fldate,
cast(price as abap.char(10)) as price_text
}

🔹 Explanation:

✅ Converts price (numeric) into CHAR(10). ✅ Useful when concatenating values or preparing for UI display.


🔹 Using CAST for Numeric Conversion

define view ZFLIGHT_CAST as select from sflight {
key carrid,
key connid,
cast( seatsocc as abap.dec(5,2) ) as seats_occupied_percent
}

🔹 Converts seatsocc to decimal (5,2) for percentage calculations.


🔹 Using CAST with CASE WHEN

define view ZFLIGHT_CATEGORY as select from sflight {
key carrid,
key connid,
case
when price > 500 then cast('Expensive' as abap.char(10))
when price > 200 then cast('Moderate' as abap.char(10))
else cast('Cheap' as abap.char(10))
end as price_category
}

🔹 Ensures CASE WHEN outputs are in consistent CHAR(10) format.


🔹 Key Benefits of CAST in CDS

✅ Ensures data type compatibility when working with calculations or joins. ✅ Converts numeric values to text for UI display. ✅ Helps standardize output formats in complex queries.

Would you like an example with date or time conversions? 🚀