Skip to Content
2
Jun 10, 2020 at 12:56 PM

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

520 Views

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?