How to Truncate a Raw String in SAP ABAP CDS View: A Complete Guide

How to Truncate a Raw String in SAP ABAP CDS View: A Complete Guide

SAP

When working with SAP ABAP CDS views, you'll often encounter scenarios where you need to manipulate string data using built-in functions like SUBSTRING, LEFT, or RIGHT. However, things get complicated when you're dealing with RAWSTRING type fields, also represented as abap.string(0) in certain standard SAP tables.

The challenge? Standard CDS string functions simply don't work with RAWSTRING data types. If you attempt to apply any string function to a RAWSTRING field, you'll encounter errors like:

Function LEFT: parameter at position 1 has incorrect data type STRG

Truncate error

The Data Type Casting Limitation

Before diving into the solution, let's understand why this happens. In SAP ABAP CDS views, data type conversion follows strict rules. According to SAP's data type casting matrix , RAW fields cannot be cast to any other data type. This fundamental limitation blocks us from using standard string manipulation functions directly.

CDS Casting Matrix

The casting restriction means we need an alternative approach - one that operates at the database level where we have more flexibility with string operations.

The Solution: CDS Table Function with AMDP

The most effective solution is to create a CDS Table Function implemented with an AMDP (ABAP Managed Database Procedure) method. This approach leverages SQL Script's native string functions, which can handle raw string manipulation that standard CDS functions cannot.

Step 1: Create the CDS Table Function

First, let's create the table function definition:

@EndUserText.label: 'Table function for CDATAF'
define table function ZT_AIF_CDATAF
returns
{
  key MANDT         : abap.clnt;
  key MSGGUID       : sxmsguid;
  key SAVE_COUNTER  : int4;
  key FIELD_COUNTER : int4;
      FIELD_NAME    : abap.char(100);
      value_new     : /aif/value;
      value_old     : /aif/value;
}
implemented by method
  zcl_aif_cdataf=>get_data;

Step 2: Implement the AMDP Class

Next, we create the AMDP class that contains the actual truncation logic:

CLASS zcl_aif_cdataf DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb .
    CLASS-METHODS: get_data FOR TABLE FUNCTION zt_aif_cdataf.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

Step 3: The Core Implementation

Here's where the magic happens - the AMDP method implementation:

Understanding the Logic

The implementation processes data from the /AIF/CDATAF table, which contains field paths in this format:

Our logic extracts the last substring after the final hyphen, giving us the actual field name (MATNR in this example). The SQL Script environment allows us to use string functions like SUBSTR_AFTER and LOCATE on raw string data, which wouldn't be possible in standard CDS views.

Using the Table Function in Other CDS Views

Once created, you can easily consume this table function's FIELD_NAME field in any other CDS view through associations or joins. Since both your main CDS view and this table function likely share the MSGGUID field, you can link them using a LEFT OUTER JOIN:

This approach allows you to seamlessly integrate the processed field data into any existing CDS view structure.

Alternative Approaches for Raw String Manipulation

While the table function approach is the most versatile, there are other methods depending on your specific use case and system requirements:

1. Custom CDS View Entity with Implementation Class

Create a CDS view entity and implement the truncation logic in ABAP within the implementation class. This method works well for OData services and SAP Fiori applications but has limited scope.​

2. Virtual Elements with Calculated Fields

Define a CDS view with virtual elements using the @ObjectModel.virtualElementCalculatedBy annotation. Write the truncation logic in ABAP within the specified class. Like the previous approach, this is primarily suitable for OData services.​

3. Analytics Cube with Read Class

For analytical scenarios, use @Analytics.dataCategory: #CUBE with @Analytics.readClassName annotation. This approach works with both OData services and SAC queries, making it suitable for reporting use cases.​

4. Table Function with AMDP (Recommended)

As demonstrated in this post, this approach offers the most flexibility. It works seamlessly with OData services, can be consumed in SAC queries, and provides the best performance for complex string operations.​

Why Choose Table Functions?

Among all available options, table functions offer several key advantages:

  • Database-level processing: Operations execute directly on the database, ensuring optimal performance

  • Broad compatibility: Works with OData services, SAC queries, and standard CDS consumption

  • SQL Script flexibility: Access to comprehensive string manipulation functions unavailable in standard CDS

  • Reusability: Once created, can be consumed by multiple CDS views across your landscape

Conclusion

Dealing with RAWSTRING data in CDS views requires a strategic approach since standard string functions are incompatible with this data type. The CDS Table Function with AMDP implementation provides an elegant, performant solution that overcomes these limitations while maintaining broad compatibility across different SAP technologies.

This approach not only solves the immediate technical challenge but also creates a reusable component that can be leveraged throughout your development landscape. Whether you're building analytical reports, OData services, or complex data transformations, this pattern will serve as a reliable foundation for raw string manipulation in your CDS architecture.


For more SAP ABAP and CDS insights, check out my other technical posts on SAP development and follow my journey on LinkedIn