Application Development 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: 

Is it possible to suppress CAST warning in CDS view definition? (NW 7.5)

mh97
Contributor
0 Kudos

We are on Netweaver 7.5. I am just starting with using CDS for production application. I have a simple CDS view where I am using CAST. In ADT I get a warning:

CAST USRID from CHAR to type NUMC: target type length too small, loss of data poss.

The source field only should ever have the target type and length. I have tested with various bad data and am fine with how it is handled (truncated).

Is it possible to suppress the warning? Is there something like a pragma for this purpose? Or is it considered a bad idea to never ever ever do, meaning I should change the view and handle the cast outside CDS? (that last doesn't sound right ...)

Please be gentle with me, as I said I am just getting started with these. 🙂

Thanks!

1 ACCEPTED SOLUTION

mh97
Contributor

Got it ... use substring first:

cast( substring( usrid, 1, 8 ) as abap.numc( 8 ) ) as my_field,

this solves the syntax warning and gives the expected result.

Thanks to anyone who gave some thought to this, I hope my resolution is helpful to you.

3 REPLIES 3

p244500
Active Contributor
0 Kudos

Hi,

you should read cast_expr, Conversion Rules

Source types CHAR and SSTRING

  • Character-like target type
The possible target types are CHAR, SSTRING, NUMC, CLNT, LANG, DATS, TIMS, UNIT, and CUKY.
  • The character string is written to the target field as a left-aligned string including leading blanks. If the target type is too short, the string is truncated on the right and a syntax check warning occurs. If the target field is too long, it is padded on the right with blanks.
  • There is no special handling for the special target type for numeric text, NUMC, and for the date/time types DATS and TIMS. It is advisable to only convert valid values. In the case of the target type NUMC, the built-in function LPAD can be used to create leading zeroes.

0 Kudos

Thanks for the link. Yes, it is a syntax warning as I am doing it, but I think I do want the result field in the view to be cast to the shorter type. Since that field in the result set is expected to be a PERNR, it will be used as a PERNR, potentially to select from another PA table in a next step of a program.

The question is whether there is a different way to do it that gets the result but avoids the syntax error? Or can I just suppress the syntax error with something like a pragma that says, "hey, I've thought about this and I really want to do it?"

mh97
Contributor

Got it ... use substring first:

cast( substring( usrid, 1, 8 ) as abap.numc( 8 ) ) as my_field,

this solves the syntax warning and gives the expected result.

Thanks to anyone who gave some thought to this, I hope my resolution is helpful to you.