In SAP ABAP CDS Views, you might encounter scenarios where a field contains leading zeros (e.g., numeric fields or strings) and you want to remove them for presentation or further processing. You can achieve this using various string manipulation functions available in CDS, such as CONCAT, REPLACE, or SUBSTRING. Below is a guide on how to remove leading zeros from a string or number in a CDS view.
1. Removing Leading Zeros from a String
If you have a string field (e.g., char_field) with leading zeros, you can use the REPLACE function in the CDS view to remove those leading zeros.
Example 1: Using REPLACE Function
define view ZFLIGHT_REMOVE_ZEROS as select from sflight { key carrid, key connid, fldate, price, // Removing leading zeros from a string field replace( char_field, '^[0]+', '' ) as char_field_no_leading_zeros}Explanation:
- The
REPLACEfunction replaces a pattern. In this case,^[0]+is a regular expression pattern that matches leading zeros at the beginning of the string and replaces them with an empty string (''). - This will remove all leading zeros from the field
char_field.
2. Removing Leading Zeros from Numeric Fields
For numeric fields (e.g., integers), leading zeros are typically not stored in the database (since they are not needed for numeric operations). However, if you still want to remove them from the presentation layer (e.g., when converting a number to a string), you can cast the number as a string and then use the REPLACE function.
Example 2: Remove Leading Zeros from a Numeric Field
define view ZFLIGHT_REMOVE_ZEROS_NUM as select from sflight { key carrid, key connid, fldate, price, // Converting number to string and removing leading zeros replace( cast( price as string ), '^[0]+', '' ) as price_no_leading_zeros}Explanation:
cast(price as string)converts the numeric fieldpriceto a string.- The
REPLACEfunction removes any leading zeros in the string representation of thepricefield.
3. Using the CONCAT Function for String Manipulation
If you’re working with string concatenation and want to remove leading zeros before combining fields, you can use the CONCAT function in combination with REPLACE.
Example 3: Removing Leading Zeros and Concatenating Fields
define view ZFLIGHT_CONCAT_REMOVE_ZEROS as select from sflight { key carrid, key connid, fldate, price, // Concatenating fields after removing leading zeros concat( replace( char_field, '^[0]+', '' ), ' - ', replace( cast( price as string ), '^[0]+', '' )) as combined_field}Explanation:
- This view removes leading zeros from both
char_fieldandpricebefore concatenating them into a new fieldcombined_field.
4. Handling Data Type Conversion and Formatting
In some cases, if you’re working with fixed-length numeric fields (e.g., CHAR(10)), you might need to handle formatting manually. You can use string functions in CDS views to achieve this.
Example 4: Using SUBSTRING for Manual Formatting
define view ZFLIGHT_REMOVE_ZEROS_SUBSTRING as select from sflight { key carrid, key connid, fldate, price, // Using SUBSTRING to remove leading zeros manually substring( char_field, length( char_field ) - length( replace( char_field, '^[0]+', '' )) + 1 ) as char_field_no_leading_zeros}Explanation:
- This approach uses
substringin combination withreplaceto calculate the length of the string without leading zeros and extract the substring accordingly.
Conclusion
To remove leading zeros in a SAP ABAP CDS view, you can use the following approaches:
REPLACEFunction: UseREPLACEwith a regular expression to remove leading zeros from string fields.CASTandREPLACE: For numeric fields, first, cast them to a string and then remove leading zeros.SUBSTRING: Manually remove leading zeros by extracting the part of the string that excludes them.