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: 

Cast CHAR to DEC in CDS View

rowieforms
Participant

Hi Experts,

As I understand its not possible to cast CHAR to DEC based on documentation from SAP.

https://goo.gl/jtFcfy

My requirement is to get the difference of 2 fields, one is an amount, the other is a CHAR type field. When I try to do the arithmetic expression, error is being raised that it you cannot subtract CHAR from amount field. This is why I want to cast the CHAR into an amount field with decimals. How can I do this? Are there any workaround?

Thanks and regards,

Rowie Formaran

1 ACCEPTED SOLUTION

rowieforms
Participant

Hi All,

This has been resolved. i used a table function and used function to_decimal to convert.

9 REPLIES 9

Sandra_Rossi
Active Contributor

Just for reference, here is the link to ABAP Documentation which says that CHAR can be converted only to CHAR, SSTRING, NUMC, CLNT, LANG, DATS, TIMS, UNIT, CUKY, and ACCP.

ThorstenHoefer
Active Contributor

you can cast char to numc and numc to dec

0 Kudos

Hi Thorsten,

NUMC doesn't have decimals right? What if the CHAR value has decimals?

0 Kudos

I have tried following sql in the HANA SQL console.
I'm not sure, if this also works with CDS views.

select 0.000 + '12.34' from dummy

0 Kudos

Thanks for your hint. I know it is not really relevant here but I needed this to convert a timestamp field in char format to the actual timestamp format in CDS.

cast ( cast ( CharTimestampField as abap.numc( 14 ) ) as timestamp ) as TimestampField,

0 Kudos

pramod.teewaree You're right, probably not relevant, so, why not posting it as a new question + answer? (anyway, you may go to the CAST documentation and you can see all permitted conversions)

rowieforms
Participant

Hi All,

This has been resolved. i used a table function and used function to_decimal to convert.

Hi,

As direct casting of char to currency is not possible, we need to cast char to numc first and then to currency. So, I converted Char to Currency , by concatenating '.00' to char value , then casting it to numc , an again casting it to Currency.

cast( cast(concat(amountchar, '.00') as abap.numc(30) ) as wrbtr_z) as Amountcurrency

Hope this helps.

0 Kudos

Cool gayathri_sunil, thanks for sharing!