How to remove leading zeros?

When I am writing to work file I see in the output txt file value of N13 that looks like this 0000000856147
Is there a way to remove the zeros easily?

You should use MOVE EDITED #field EM=ZZZZZZZZZZZZ9 #filed-workfile.
See Documentation here :
MOVE (softwareag.com)

1 Like

Hi,
Yes there is a way.
compress numeric f1 to rec

can you elaborate?

See the documentation
When compress numeric is performed its trim leading zeros.
But, you should take consider what is the use of the wf.
If another process needs to read by fixed length, then you will have a problem,
if the wf is used for creating a report (Ex: csv file), then it will be ok.

How are you writing your workfile? Is it Fixed or Variable? Do you have a single text field redefined into it’s components, or are you writing the fields directly?

Example 1: single text string redefined:

DEFINE DATA LOCAL
1 #WKF (A100)
1 REDEFINE #WKF
  2 NAME (A25)
  2 NUMBER (N13)
END-DEFINE

READ WORKERS BY NAME
  MOVE WORKERS.NAME TO #WKF.NAME
  MOVE WORKERS.NUMBER TO #WKF.NUMBER
  WRITE WORK FILE 1 #WKF
END-READ
END

Because NUMBER is N13, it will appear right-justified and zero-padded in the redefinition. Note that negative numbers are different. In a negative number, the last digit will be another character that includes both the value of the one’s digit and the negativity of the number. This gets into bits, bytes, and nibbles and is probably covered somewhere else.

If you want NUMBER to not be zero-padded, you will need to change it to (A13), or (A14) if the number might be negative. With no other changes to the code #WKF.NUMBER will no longer have leading zeros. If you want more control, use MOVE EDITED.

Example 2: write fields directly

READ WORKERS BY NAME
  WRITE WORK FILE 1
    WORKERS.NAME
    WORKERS.NUMBER
END-READ
END

Again, WORKERS.NUMBER is N13 so it will be written to the work file as above.

To fix this, you will need to not write WORKERS.NUMBER to the work file, but create a text field to move the numeric field to as in example 1.

But as Hezi points out, you need to know who is reading the file and what they are expecting. If I try to read this workfile in Natural You will run into a problem if the program is expecting an N13 field and you write it out as A13.

1 Like

This is the code you need:

string strInput = "0001234";
strInput = strInput.TrimStart('0');

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.