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

Comparing character strings

konstantinos_vassiliadis
Active Participant
0 Likes
3,771

Hello,

I have the following issue which I am trying to tackle but I do not find a solution.

I have a Z database table  which, among others, has the following column:

VENUM with data type c(10). " data element VENUM

Also in my program I store the Z database table values into an internal table ITAB_ZMIS_HU_501 with the following structure

venum(10) TYPE c

I notice during debugging that the internal table has the value e.g. 0003867000 when the Z table has the column value 3867000.

Also, I want to check in my program whether a specific value is found in at least one line of the Z table. So I do the following

data: begin of itab_noncancelled_hu OCCURS , " to store handling units that are not cancelled

         VHILM(18) TYPE c,    " Packaging material

         ...

       END OF itab_noncancelled_hu.


wa_itab2 like LINE OF ITAB_NONCANCELLED_HU ,


READ TABLE ITAB_ZMIS_HU_501 WITH TABLE KEY venum = wa_itab2-venum  TRANSPORTING NO FIELDS.

       if sy-subrc NE 0. " Row is not found in ITAB_ZMIS_HU_501

         " do something here

       ENDIF.

The READ returns sy-subrc = 4 (row not found) because  wa_itab2-venum has value 0000003867 and ITAB_ZMIS_HU_501-VENUM is 0003867000.

How can I avoid this and do a proper string comparison ignoring leading and trailing zeros? Why is this happening - I mean different representations although all are defined of the same data type?

Apologies for the long email but I cannot find a solution.

9 REPLIES 9
Read only

Former Member
0 Likes
1,690

Hello.

First of all, the values are 0003867000 and 3867000

or as u said " The READ returns sy-subrc = 4 (row not found) because  wa_itab2-venum has value 0000003867 and ITAB_ZMIS_HU_501-VENUM is 0003867000."

The second sentence, i guess is wrong...

If you need to add zero 000 to ur value, use FM CONVERSION_EXIT_ALPHA_INPUT, otherwise use CONVERSION_EXIT_ALPHA_OUTPUT .

Regards


Read only

0 Likes
1,690

Hi,

Maybe I was not clear in my description. Let me try again

I have following declaration of internal table:

data: BEGIN OF itab_ZMIS_HU_501 OCCURS ,

         venum(10) TYPE c ,

       END OF itab_ZMIS_HU_501.

Also, I have created a database table Z with following structure:

Field               Data element          Data Type      Length

------------------------------------------------------------------------

VENUM                VENUM                    CHAR          10

At some point in my program I insert a row in the Z table with the following line:

DATA: BEGIN OF itab_venum OCCURS , " to store the VENUMS of units to be posted

         VENUM(10) TYPE c" Handling unit number - client-specific

         ..............................

       END OF itab_venum.

data wa LIKE LINE OF itab_venum.

   loop at ITAB_VENUM into wa.

     if ( wa-MOVE_TYPE = '501' ).

       modify ZMIS_HU_501 from wa.

     endif.

endloop.

Then I retrieve all Z to itab as follows:

" copy all z table to internal table

   select *

   into CORRESPONDING FIELDS OF TABLE ITAB_ZMIS_HU_501

   FROM ZMIS_HU_501.

Then I want to check if a value is found in ITAB_ZMIS_HU_501 as follows:

READ TABLE ITAB_ZMIS_HU_501 WITH TABLE KEY venum = wa_itab2-venum TRANSPORTING NO FIELDS.

The problem is that although there is the value I search into Z table, the read fails (sy-subrc = 4) because

ITAB_ZMIS_HU_501-VENUM is 0003867000

wa_itab2-VENUM is 0000003867

(and Z table column value is 3867000)

I hope I am clearer now. I do not want the READ TABLE to fail if the character string is in the table. Why does it add 6 zeros in the work area??





Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,690

Hi Konstantinos,

From the code snippet you have posted, it is not clear where are you filling the work area wa_itab2. Since apparently the value in wa_itab2-venum is incorrect, you need to look at the code that is filling this workarea.

Having said that, it's a little strange to see the wa_itab2-venum has the zeroes at the end truncated and moved in front. Is there some 'shifting' happening here?

Read only

0 Likes
1,690

Did you try to define it as:

VENUM TYPE VENUM

The reason there are leading zeroes is because the data element/domain has a built in conversion exit.

Read only

Former Member
0 Likes
1,690

Hi Konstantinos Vassiliadis,


Use the CONVERSION_EXIT_ALPHA_OUTPUT function module and FYI PFBL ,

http://wiki.sdn.sap.com/wiki/display/ABAP/Field+Catalog+Options+In+ALV

http://scn.sap.com/thread/911301

the below link will explain with example please check it .

https://scn.sap.com/message/5673173

I hope you can understand.

Regards

Mahesh

Read only

siva_subramanian2
Participant
0 Likes
1,690

Hi

Use Conversion routine before your READ statement or Pack the variable using PACK keyword before READ statement.

Thanks

Siva

Read only

vigneshyeram
Active Participant
0 Likes
1,690

This message was moderated.

Read only

Former Member
0 Likes
1,690

Hi Konstantinos,

The above problem is because of this -

You have declared VENUM as VENUM in you Z table. If you check the Domain VENUM (Definition Tab), it has a Conversion Exit ALPHA which is adds/deletes leading zeroes. Hence the custom table value is without leading zero. Maybe while the value is getting populated into the internal table, this Conversion Exit is passing it with leading zeroes.

You can correct this by the following options:

1. Declare VENUM in your program as ZCUSTOMTAB-VENUM where ZCUSTOMTAB is your custom table name. OR

2. Declare VENUM in your custom table as CHAR 10 and not VENUM.

Hope this helps!

Read only

Former Member
0 Likes
1,690

Hi Konstantinos,

You may use the FM CONVERSION_EXIT_ALPHA_OUTPUT to remove the leading zeros, but it is sure that it again fails at your functionality.

Instead of READ TABLE, you may have to loop the internal table and compare the fields.

There you can use the SHIFT { {LEFT DELETING LEADING}     | {RIGHT DELETING TRAILING} } pattern ... .

where pattern is '0'. Try this, it may help you.

Regards,

Akhil