While working recently with the SHIFT LEFT DELETING LEADING statement, I sometimes received what I felt were incorrect results. I decided to carry out some additional research into the statement and found that my actual understanding of what the statement was supposed to do and how it works was incorrect.
Let me show you my findings – what I thought at the beginnng and what I now know!
I began with reviewing the online documentation for the SHIFT statement:
I then wrote a simple ABAP Program to test the statement:
I ran the program and this was the generated output:
All appeared fine – however, when I looked a little closer, I found that the output from the last test on g_var6 – wasn’t what I had expected.
I thought I would be returned: ZMVPZSBU… but the initial Z character was missing! Where had it disappeared to?
In search of the missing Z!
I showed my coding and output to a colleague, he was also unsure why the output seemed incorrect. So, I went back to the documentation and reviewed it again. Then it hit me! I had been trying to see something that wasn’t there! Here is what I found:
- The characters listed after the LEADING statement are treated as a pattern. These are not considered as a sequence of characters which should match, i.e. they are not a substring
- The content of the field is moved as long as the first or last character is contained in the pattern
So for g_var6:
- The SHIFT statement was not comparing the first n characters with the given substring ‘0106ZUSA’
- Instead it was checking each first character of g_var6 to see if it was contained in the pattern ‘0106ZUSA’. If yes, then it was removed and the rest shifted
- As the second Z of 0106ZUSAZMVPZSBU is also contained in ‘0106ZUSA’, it was also removed. The next first character is M, which is not contained in the pattern provided, hence the SHIFT is finished at this position
When I started working with the SHIFT statement, I was actually treating it like a FIND statement. However, this was incorrect!
- If you want to work with substrings – use the FIND statement!
- If you want to work with patterns – use the SHIFT statement!