Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP CDS View Cast in Case Expression

Former Member
36,565

Hello,

I want to make a new field in my CDS-View, with a CASE-condition, like that:

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

The problem is that the type of the field "usage_6m" 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 in this position like this:

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

...

Do any of you have an idea how I cand solve this, or why I can't cast a field inside of a CASE-expression.

Thank you!

Cristina

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
15,865

Could you consider NUMC10 as a string giving a simpler

CASE
  WHEN usage_6m = '0000000000' then '1'
  WHEN usage_6m < '0000000010' then '2'
  WHEN usage_6m < '0000000250' then '3'
  ELSE '0'
END AS usage_icon

Regards,
Raymond

6 REPLIES 6
Read only

RaymondGiuseppi
Active Contributor
15,866

Could you consider NUMC10 as a string giving a simpler

CASE
  WHEN usage_6m = '0000000000' then '1'
  WHEN usage_6m < '0000000010' then '2'
  WHEN usage_6m < '0000000250' then '3'
  ELSE '0'
END AS usage_icon

Regards,
Raymond

Read only

0 Likes
15,865

Whoa, that was an easy workaround! Thank you!

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
15,865

Hi Cristina,

Sorry, but the searched CASE does not support expressions as LHS, see docu.

You have to find a workaround e.g. as Raymond suggested.

Read only

ravilla
Explorer
0 Likes
15,865

Hello Sir,

i have also got same requirement, how did you achieve.please let me know, its great thankful to you if you would give early response.

Thanks

Ravilla

Read only

0 Likes
15,865

Hi Cristina,

Take your usage_6m as string and then try . Hope it works.

Regards

Ram

Read only

Former Member
0 Likes
15,865

Requirement Show Company Code from Table 1(o) if exist else if show Company code from table2(P) if exist elseif show it from Table 3(Q).

Data Type of Company code is different from table 1(o) or Table2 (p) Let us say CHAR 04 and table3 (Q) company code CHAR 20

For Table 1 and 2 we can write the below code

Case

when Q.lsa_bukrs is null then p.company_code

else Q.lsa_bukrs end as Company_code2,

For 3 table I wrote below code but it does not work because the Datatype of Company code is different in Table 3(Q)

Case

when t.org_unit_key is not null and Q.lsa_bukrs is null and p.company_code is null

then t.org_unit_key

when Q.lsa_bukrs is not null then Q.lsa_bukrs

when Q.lsa_bukrs is null and p.company_code is not null then p.company_code

end as Company_code2,

Then I went and Opted for another Approach Making one more CDS view Comparing two table and In Main cds View comparing the 3rd table and result of first View

Additional view

{ key a.lsa_id as lsa_id,

cast( Q.lsa_bukrs as abap.char(20))as Q_lsa_bukrs,

cast( p.company_code as abap.char(20))as p_company_code,

cast(case

when Q.lsa_bukrs isnull// Purchase Order

then p.company_code

else Q.lsa_bukrs

endas abap.char(20))as QP_Company_code,

cast( t.org_unit_key as abap.char(20))as t_Company_code,

Q.gsber_sc as Lsa_Business_area

}

View Main: In this View Compare the company code which u received from first View and Table 3 (Q) Company Code

{key U.QP_Company_code,

U.t_Company_code,

casewhen U.QP_Company_code isnull

then U.t_Company_code

else U.QP_Company_code

endas Company_code,

}