Search code examples
castingabapcds

Casting column inside CASE is not supported


I want to make a new field in my CDS-View, with a CASE-condition as below

case  
  when usage  = 0 then '1'
  when usage < '10' or usage_6m > '0' then '2'
  when usage < '250' or usage_6m > '10' then '3'
  else '0'
end as usage

The problem is that the type of the field "usage" NUMC10 is, so eclipse throws an error : "data type conflict". So then I wanted to cast the field to abap.int4, but that does not work eighter, because the CAST--expression is not supported:

case 
        when cast( usage  as abap.int4 )  = 0 then '1'
...

Experts please advise how can i solve this, why I can not cast a field inside of a CASE-expression.

regards,
Umar Abdullah


Solution

  • You can also compare the NUMC values directly. The help article "ABAP CDS - cond_expr, Comparable Types", says that NUMC can be compared to other NUMC literals with the relational operators >, < etc. if the field and literal have the exact same length.

    Meaning you need to change this for your NUMC(10) to:

    case  
      when usage = '0000000000' then '1'
      when usage < '0000000010' or usage_6m > '0' then '2'
      when usage < '0000000250' or usage_6m > '10' then '3'
      else '0'
    end as usage