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

Explicit type conversion functions

Former Member
0 Likes
1,838

I need to convert a char value returned by the select query to number.

Select max(to_number(child_id))

from table . . ..

i don't know what function is used in Minisap for this conversion of character to Number. Remember i am using Minisap that is i think 46C or 46D.

I find out a function NUM(a) from SAP help portal but that doesnot work with the release i am using.

Can any body help me plz.

Regards

Zahid

9 REPLIES 9
Read only

Former Member
0 Likes
1,297

Hi Zahid,

I'm slightly surprised with this question. Look at this code snippet.

data : integer type i,
       string  type string.

move '12345' to string.

write string.

integer = string.
write : / integer.

If the character string is convertible to an integer, there will be no problem.

But if you have some like -

data : integer type i,
       string  type string.

move '12A345' to string.

write string.

integer = string.
write : / integer.

You will get a short dump, because 123A45 cannot be converted to an integer.

Of course, you can handle this runtime error by using the CATCH...ENDCATCH block for exception handling in ABAP .

Regards,

Anand Mandalika.

Read only

0 Likes
1,297

Dear Anand!

i knew all this but u don't understand what i asked. what you are suggesting is implicit conversion, but unfortunately that does not help in my situation.

in fact i have a character column "CHILD_ID" in the table "ZTOPIC". now i want to get the maximium child_id entered in the table. The values in the child_id column are:

1

2

3

4

5

6

7

8

9

10

11

12

This query does help

Select max( child_id )

into ...

from ...

The maximum should give me 12 but it gives me 9 because its a character column.

now my problem could be solved by converting the child_id to number first and then apply the max function over it . . i did this hundreds of times in oracle like this

select max(to_number(child_id))

into ...

from ....

the to_number function first converts the column values to number and then the max function is applied over those values.

My question is is there any function for Char to Number explicit conversion.

Regards

Zahid.

Read only

0 Likes
1,297

Hello Zahid,

I'm sorry for misunderstanding your question. But there's something that I must say here. Obviously, ZTOPIC is a custom table and it has been designed poorly. It has not been anticipated that a user could possible issue an <i>arithmetic</i> query on these fields.

In this case, it is the MAX( ) that you're using in the SELECT query. The field here is expected to be numeric in nature. And unfortunately, within Open SQL you simply cannot use the kind of explicit run-time type conversion that you are talkling about.

Even if you get the data in an internal table, you will have to do some processing to get the right result.

But it appears that there's a (permanent) solution that you can use for this problem. Change the type of the field in the dictionary to NUMC. and you will get the desired result. the database will not allow you to do it directly - you have to use the database utility for that.

If you don't want to do this, then select all the data into an internal table (in which this field is declared with type N ), sort by this field and get your answer.

Regards,

Anand Mandalika.

Read only

0 Likes
1,297

Hi,

U can use this function module

data: numc TYPE C.

CALL FUNCTION 'CHAR_NUMC_CONVERSION'

EXPORTING

INPUT = 6

IMPORTING

NUMCSTR = numc

.

This will convert char to numc.

Again if u want to convert it into char use

CALL FUNCTION 'COC1_CONVERT_VALUES_TO_CHAR'

EXPORTING

CHARACTERISTIC_VALUE = ' '

  • CHARACTERISTIC_VALUE_FLOAT = ' '

  • FORMAT = 'CHAR'

IMPORTING

CHARACTERISTIC_VALUE_CHAR =

  • EXCEPTIONS

  • FORMAT_NOT_VALID = 1

  • OTHERS = 2.

Hope this helps.

Thanks & Regards,

Judith.

Read only

0 Likes
1,297

can we use a function module in a select statement ?

Read only

0 Likes
1,297

Read only

0 Likes
1,297

You have got a pretty nice smiling face.

should i reward you 10 points for this smile?

Read only

0 Likes
1,297

Thanks (for the compliment), but no thanks (for your 10 points).

It does all of us a lot of good to have some humour in life, doesn't it ?

Read only

0 Likes
1,297

Hi Zahid,

Basically you are stuck on this one.

With OPEN SQL you can't introduce ABAP functions into the select statement.

If you really wanted to do this in SQL you could use your oracle statement within an EXEC SQL. ENDEXEC. pairing.

Otherwise, you are going to have to redefine your ZTOPIC table to make CHILD_ID numeric, or load all the data into an ABAP internal table and work out the maximum value there.

Not sure this is the answer you want to hear, but I don't see another way around it.

Brad