All skills
secondsky avatar

/sap-abap-cds

@620a19a
by Eddiesecondsky/sap-skills456 stars
120

Comprehensive SAP ABAP CDS (Core Data Services) reference for data modeling, view development, and semantic enrichment. Use when creating CDS views or view entities, defining data models with annotations, working with associations and cardinality, implementing input parameters, using built-in functions, writing CASE expressions, implementing access control with DCL, handling CURR/QUAN data types, troubleshooting CDS errors, querying CDS views from ABAP, or displaying data with SALV IDA. Covers ABAP 7.4+ through ABAP Cloud.

Use this Skill: https://skilld.dev/gh/secondsky/sap-skills/sap-abap-cds

This session only. Nothing lands on disk.

referencesfunctions-reference.md

≈2.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

ABAP CDS Built-in Functions Reference

Complete reference for all built-in functions available in ABAP CDS views.

Source: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/abencds_f1_builtin_functions.htm

Version Note: concat(), substring(), replace(), length() are available from 7.40 SP08. upper() and lower() require 7.51+. Date/time conversion functions (tstmp_to_dats, dats_tims_to_tstmp, etc.) require 7.51+. Currency/unit conversion functions are available from 7.40 SP08. Numeric functions (abs, ceil, floor, round, division) are available from 7.40 SP08.


String Functions

concat(arg1, arg2)

Concatenates two strings.

concat(first_name, last_name) as FullName
-- 'John' + 'Doe' = 'JohnDoe'

concat_with_space(arg1, arg2, spaces)

Concatenates strings with specified number of spaces.

concat_with_space(first_name, last_name, 1) as FullName
-- 'John' + 'Doe' = 'John Doe'

length(string)

Returns the length of a string.

length(description) as DescLength
-- 'Hello' = 5

left(string, n)

Returns leftmost n characters.

left(material_number, 4) as MaterialPrefix
-- '12345678' = '1234'

right(string, n)

Returns rightmost n characters.

right(material_number, 3) as MaterialSuffix
-- '12345678' = '678'

substring(string, pos, len)

Extracts substring from position for length.

substring(document_number, 3, 5) as SubDoc
-- '1234567890' starting at 3 for 5 = '34567'

Note: Position is 1-based.

upper(string)

Converts to uppercase.

upper(name) as NameUpper
-- 'Hello World' = 'HELLO WORLD'

lower(string)

Converts to lowercase.

lower(name) as NameLower
-- 'Hello World' = 'hello world'

lpad(string, length, pad_char)

Left-pads string to specified length.

lpad(document_number, 10, '0') as PaddedDoc
-- '12345' = '0000012345'

Note: If original length exceeds target, string is truncated.

rpad(string, length, pad_char)

Right-pads string to specified length.

rpad(name, 20, ' ') as PaddedName
-- 'John' = 'John                '

ltrim(string, trim_char)

Removes characters from left side.

ltrim(document_number, '0') as TrimmedDoc
-- '0000012345' = '12345'

rtrim(string, trim_char)

Removes characters from right side.

rtrim(name, ' ') as TrimmedName
-- 'John     ' = 'John'

replace(string, old, new)

Replaces all occurrences of substring.

replace(phone, '-', '') as PhoneClean
-- '123-456-7890' = '1234567890'

instr(string, substring)

Finds position of substring (0 if not found).

instr(email, '@') as AtPosition
-- 'user@example.com' = 5

Numeric Functions

abs(number)

Returns absolute value.

abs(amount) as AbsoluteAmount
-- -100 = 100
-- 100 = 100

ceil(number)

Rounds up to nearest integer.

ceil(price) as CeilingPrice
-- 5.1 = 6
-- 5.9 = 6
-- -5.1 = -5

floor(number)

Rounds down to nearest integer.

floor(price) as FloorPrice
-- 5.1 = 5
-- 5.9 = 5
-- -5.1 = -6

round(number, decimals)

Rounds to specified decimal places.

round(price, 2) as RoundedPrice
-- 5.567 with 2 decimals = 5.57
-- 5.564 with 2 decimals = 5.56

Negative decimals: Round to left of decimal point

round(12345, -2) as RoundedThousands
-- 12345 = 12300

div(dividend, divisor)

Integer division (truncates decimal).

div(total_minutes, 60) as Hours
-- 125 / 60 = 2

division(dividend, divisor, decimals)

Division with specified decimal precision.

division(10, 3, 4) as Result
-- 10 / 3 with 4 decimals = 3.3333

mod(dividend, divisor)

Returns remainder (modulo).

mod(total_minutes, 60) as RemainingMinutes
-- 125 mod 60 = 5

Date and Time Functions

dats_add_days(date, days)

Adds days to date.

dats_add_days(order_date, 7) as DeliveryDate
-- '20241115' + 7 = '20241122'

Negative days: Subtract days

dats_add_days(order_date, -30) as PreviousMonth

dats_add_months(date, months)

Adds months to date.

dats_add_months(start_date, 1) as NextMonth
-- '20241115' + 1 = '20241215'

dats_days_between(date1, date2)

Returns days between two dates.

dats_days_between(order_date, delivery_date) as LeadTime
-- '20241101' to '20241115' = 14

Note: Returns negative if date1 > date2.

dats_is_valid(date)

Validates date (returns 1 or 0).

dats_is_valid(input_date) as IsValidDate
-- '20241115' = 1
-- '00000000' = 0
-- '20241332' = 0

Timestamp Functions

tstmp_add_seconds(timestamp, seconds, fail)

Adds seconds to timestamp.

tstmp_add_seconds(created_at, 3600, 'FAIL') as OneHourLater

tstmp_seconds_between(ts1, ts2, fail)

Seconds between timestamps.

tstmp_seconds_between(start_ts, end_ts, 'FAIL') as DurationSeconds

tstmp_current_utctimestamp()

Current UTC timestamp.

tstmp_current_utctimestamp() as CurrentTimestamp

Note: The third parameter in timestamp functions controls error handling. Use 'FAIL' to propagate errors if timestamps are invalid, or 'NULL' to return NULL on error. Ensure timestamps are in valid format (YYYYMMDDHHMMSS) before using these functions.


COALESCE Function

coalesce(arg1, arg2, ...)

Returns first non-null argument.

coalesce(customer_name, 'Unknown') as DisplayName
-- NULL = 'Unknown'
-- 'John' = 'John'

coalesce(override_price, standard_price, 0) as FinalPrice
-- First non-null value

CAST Expression

Syntax

cast(expression as data_type)

ABAP Data Types

Type Syntax Example
Character abap.char(n) cast(num as abap.char(10))
Numeric text abap.numc(n) cast(num as abap.numc(8))
Integer abap.int4 cast(str as abap.int4)
Date abap.dats cast(str as abap.dats)
Time abap.tims cast(str as abap.tims)
Currency key abap.cuky cast('EUR' as abap.cuky)
Currency amount abap.curr(n,d) cast(num as abap.curr(15,2))
Unit of measure abap.unit(n) cast('KG' as abap.unit(3))
Quantity abap.quan(n,d) cast(num as abap.quan(13,3))
Decimal abap.dec(n,d) cast(num as abap.dec(11,2))
String abap.string cast(char as abap.string)
Raw abap.raw(n) cast(hex as abap.raw(16))

Examples

-- Convert number to text with leading zeros
lpad(cast(document_number as abap.char(10)), 10, '0') as FormattedDoc,

-- Fixed currency literal
cast('EUR' as abap.cuky) as DefaultCurrency,

-- Decimal precision
cast(amount as abap.curr(15,2)) as FormattedAmount

PRESERVING TYPE

Maintain original type definition:

cast(field as abap.char(10) preserving type) as TypedField

Aggregate Functions

Used with GROUP BY clause.

sum(field)

Sum of values.

sum(amount) as TotalAmount

avg(field)

Average of values.

avg(price) as AveragePrice

min(field)

Minimum value.

min(order_date) as FirstOrder

max(field)

Maximum value.

max(order_date) as LastOrder

count(*)

Count rows.

count(*) as RowCount

count(distinct field)

Count distinct values.

count(distinct customer) as UniqueCustomers

Example with GROUP BY

define view Z_ORDER_SUMMARY as select from vbap
{
  vbeln,
  sum(netwr) as TotalAmount,
  count(*) as ItemCount,
  min(erdat) as FirstItemDate,
  max(erdat) as LastItemDate,
  avg(netwr) as AverageAmount
}
group by vbeln
having sum(netwr) > 1000

Special Functions

decimal_shift(amount, currency)

Shifts decimal based on currency decimals.

decimal_shift(amount => netwr, currency => waers) as ShiftedAmount

unit_conversion(quantity, source_unit, target_unit)

Converts units of measure.

unit_conversion(
  quantity => menge,
  source_unit => meins,
  target_unit => 'KG'
) as QuantityInKG

currency_conversion(amount, source_currency, target_currency, date)

Converts currency amounts.

currency_conversion(
  amount => netwr,
  source_currency => waers,
  target_currency => 'EUR',
  exchange_rate_date => erdat
) as AmountInEUR

Platform Note: unit_conversion and currency_conversion are primarily optimized for SAP HANA environments and may have limited support on other database platforms. These functions require the appropriate conversion tables (T006, TCURR) to be maintained. Verify availability with your system administrator before using in production queries.


Complex Function Examples

Time Formatting (Minutes to HH:MM)

concat(
  concat(
    lpad(ltrim(cast(div(flight_time, 60) as abap.char(12)), '0'), 2, '0'),
    ':'
  ),
  lpad(ltrim(cast(mod(flight_time, 60) as abap.char(12)), '0'), 2, '0')
) as FormattedTime
-- 125 minutes = '02:05'

Date Validation with Default

case
  when dats_is_valid(input_date) = 1 then input_date
  else '00000000'
end as ValidatedDate

Conditional Text Concatenation

case
  when last_name is not null
    then concat_with_space(first_name, last_name, 1)
  else first_name
end as DisplayName

Percentage Calculation

case
  when total > 0
    then division(part * 100, total, 2)
  else cast(0 as abap.dec(5,2))
end as Percentage

Function Nesting

Functions can be nested for complex transformations:

upper(
  replace(
    ltrim(
      concat_with_space(first_name, last_name, 1),
      ' '
    ),
    ' ',
    '_'
  )
) as NormalizedName
-- '  John Doe  ' = 'JOHN_DOE'

Best Practices

  1. Use COALESCE for defaults: Avoid null in calculations
  2. CAST for type safety: Explicit types prevent runtime errors
  3. Aggregate with care: Always include GROUP BY for non-aggregated fields
  4. Validate dates: Use dats_is_valid before date arithmetic
  5. Consider performance: Complex functions may impact query time

Documentation Links

Last Updated: 2025-11-23

Source: SKILL.md on GitHub

1 warning15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    The skill provides comprehensive reference material, templates, and troubleshooting guides for SAP ABAP CDS development. It consists entirely of documentation and static templates without executable code. All external references point to official SAP resources and well-known community repositories.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer6mo

    5/12 files flagged

Signed by skilld at 620a19a. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "maintainer": "Eduard Jiglau",
  "maintainer_email": "hello@sap-ai-skills.com",
  "website": "https://sap-ai-skills.com",
  "version": "2.4.1",
  "last_verified": "2026-04-02",
  "abap_release": "7.4 SP8+ / 7.50+ / ABAP Cloud",
  "sources": [
    "https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abencds.html",
    "https://github.com/SAP-samples/abap-cheat-sheets"
  ],
  "keywords": [
    "ABAP CDS",
    "Core Data Services",
    "CDS view",
    "CDS view entity",
    "define view",
    "DDL",
    "DCL",
    "annotations",
    "@AbapCatalog",
    "@AccessControl",
    "@EndUserText",
    "@Semantics",
    "@UI",
    "@Consumption",
    "@ObjectModel",
    "associations",
    "cardinality",
    "path expressions",
    "input parameters",
    "WITH PARAMETERS",
    "built-in functions",
    "CASE expression",
    "CAST",
    "session variables",
    "GROUP BY",
    "HAVING",
    "joins",
    "access control",
    "DEFINE ROLE",
    "pfcg_auth",
    "SALV IDA",
    "Eclipse ADT",
    "CDS annotations",
    "Fiori Elements",
    "OData",
    "RAP",
    "currencyCode",
    "unitOfMeasure",
    "SD_CDS_ENTITY105"
  ]
}

README badge

README badge for secondsky/sap-skills/sap-abap-cds