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: 

Convert String to Type N - standard but non-intuitive!

matt
Active Contributor
1,031

OK, we know that if you have a numeric type and try to assign a string value to it, any non-numerals will be ignored. I.e.

DATA mynum TYPE n LENGTH 4.
DATA mystring TYPE string VALUE 'DI01'.mynum = mystring.

mynum will now contain the value 0001.

We also know that when you use READ on a internal table with a key, the supplied value is converted to the type of the key. I've an internal table (HASHED), keyed on a four length numeric field. It has a record for 0001. I was confused for some time that this code

READ TABLE mytable INTO DATA(record) WITH TABLE KEY numckey = mystring.
had sy-subrc = 0, and returned a record with record-numckey = '0001'.

How would you deal with this?
  1. Have the hashed table key be a character/string field instead?
  2. Check that mystring only contains numerics?
  3. Something else?
1 ACCEPTED SOLUTION

gasparerdelyi
Advisor
Advisor
510

If you perform an extended check (SLIN) you will get a warning like the following. Unfortunately only in extended check...

Syntax check warning.
This warning is only displayed in SLIN
The assignment "NUMCKEY" = ... is not usually the equivalent of a comparison. It is best to use the same type for the c
Internal message code: MESSAGE GC6
Deactivatable using pragma ##WARN_OK. Message Code WRN 1305

9 REPLIES 9

MateuszAdamus
Active Contributor
510

Hi Matthew

Why is there a difference between these fields? Is it by design or some oversight?

Kind regards,
Mateusz

Tomas_Buryanek
Active Contributor
510

It is more like opinion, than answer:

  1. First part - huh, I admit I did not even know it works like this. And personally I would not build any functionality around this behavior.
  2. How to deal with it - I would be primarily careful to not compare apples with oranges. I mean there is not problem in READ or conversion, but in how it actually happen that you have nonnumerical mystring and use it to read numerical key...
-- Tomas --

matt
Active Contributor
510

Opinions are good!

I did know it. But only in relation to the fact that in LOOP AT WHERE, type conversions are a performance hit.

DATA test TYPE string.
DATA itab TYPE ... (table that has a char key).
LOOP AT itab ... WHERE key = test.

is more expensive than

DATA test TYPE string.
DATA itab TYPE ... (table that has a char key).

DATA mykey TYPE key_type.
mykey = test.
LOOP AT itab ... WHERE key = mykey.

In this particular instance, it never occurred to me that the key of the table (which I didn't define!) wouldn't be C4 rather than N4.

It's not a bug - more a feature of the ABAP language. And far too late to change now!

But if you used CONV for example, you'd get the same issue. What we really need is a IF type(thing) IS NUMC. I know we can use RTTS or DESCRIBE, but it's not the same.

Just for info, I changed the code as follows.

IF blah CO |0123456789|.

matt
Active Contributor
0 Kudos
510

Someone else's design. It was wrong from the start, but I've only been working here 15 years. That decision was taken 20 years ago!

Sandra_Rossi
Active Contributor
510

I like your solution - (>>DELETED--although it may surprise people as blah is of type N--DELETED<< as per Matthew comment):

IF blah CO |0123456789|. " or '0123456789'

You can also do that (but I think it's worse than your solution):

TYPES : ty_n TYPE n LENGTH 4,
        BEGIN OF ty,
          key TYPE ty_n,
        END OF ty,
        tt   TYPE HASHED TABLE OF ty WITH UNIQUE KEY key.
FIELD-SYMBOLS <c> TYPE c.
DATA: mykey TYPE ty_n,
      mystring TYPE string.

DATA(mytable) = VALUE tt( ( key = '0001' ) ).

mystring = 'DI01'.
ASSIGN mykey TO <c> CASTING.
<c> = mystring. " <==== mykey of type N will contain the invalid value 'DI01'

READ TABLE mytable WITH TABLE KEY key = mykey TRANSPORTING NO FIELDS.

ASSERT sy-subrc <> 0.

matt
Active Contributor
510

Just to be clear blah was my string value.

gasparerdelyi
Advisor
Advisor
511

If you perform an extended check (SLIN) you will get a warning like the following. Unfortunately only in extended check...

Syntax check warning.
This warning is only displayed in SLIN
The assignment "NUMCKEY" = ... is not usually the equivalent of a comparison. It is best to use the same type for the c
Internal message code: MESSAGE GC6
Deactivatable using pragma ##WARN_OK. Message Code WRN 1305

matt
Active Contributor
0 Kudos
510

Unfortunately, ATC is a bit ... broken thanks to a poor implementation of a 3rd party product. I don't think we've got SLIN directly in Eclipse - but if I'm wrong, please tell me!

I wonder if there's a way of getting SLIN warnings inside the editor?

0 Kudos
510

There is no SE80-like integration, true.

What is possible (kind of crawling back through the window if entry via the entrance door is not possible) is to execute transaction SLIN in the Eclipse environment (Ctrl-Shift-A, type SLIN, pick transaction, then push test transaction button) as long as you have all the authorizations for these steps... You will need to pass a report name (so knowing the technical report names for function groups with SAPL or for classes with the ==CP style suffix is important) -- at least there is a value help for the report name.