Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Extracting all characters of a postcode before the space

I am trying to extract the first characters from a postcode into a separate field that can then be used as a parameter. If i search on postcodes starting with (for example) M1, this is bring out not only M1 but also M11, M12, etc.

I have found a While Constructs formula example in my training book that extracts the Surname from a field containing the FirstName and Surname. I have recreated this and applied to my postcode field but it is bring out the characters AFTER the space in the postcode and not the characters BEFORE.

Does anybody know how to reverse the following so that it extracts the characters BEFORE the space?

stringVar PCode := {ADDRESS.POSTCODE};
numberVar i:=1;
numberVar Strlen := length (PCode);
numberVar Result:= -1;

while i <= Strlen and Result= -1 do
    stringVar Position:= Mid (PCode,i,1);
    if Position = " " then
        Result:= i;
        i:= i + 1;
stringVar PostSearch := mid (PCode, Result+1, (Strlen-Result));

Former Member

Hi Tracy,

Here's a really simple way to do this:

{ADDRESS.POSTCODE} [1 to (InStr ({ADDRESS.POSTCODE}, " ") - 1)];

The InStr function searches for a space and return the position of the space. This will allow us to extract the characters from position 1 to the (space - 1).

Hope this helps,


Edited by: Brian Dong on Oct 14, 2008 4:29 PM

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question