×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Extracting from string

## Extracting from string

(OP)
Hello,
I have a request to extract building code from a string. The problem is they want everything to the left of the last - in the string.
For example, the field could be Bu1-Bu2-Supervisor Shipping or it could just be Bu1-Welder or it can be completely blank.
I guess I want to extract everything before the last - in the field (like Bu1-Bu2 or something like that for the first example.
Any ideas?

### RE: Extracting from string

I'm not convinced my solution is the most elegant, but it seems to work using this formula:

#### CODE --> {@RESULT}}

WhilePrintingRecords;
Local NumberVar u := UBound(Split({Table.Column}, '-'));
Local NumberVar i;
Local StringVar R;

If      u = 0
Then    R := {Table.Column}
Else    R := R;

If      u = 1
Then    R := Split({Table.Column}, '-')[1]
Else    R := R;

If      u > 1
Then    (
For i := 1 to (u-1) step 1 do
R := R + Split({Table.Column}, '-')[i] + '-';
);

If      u > 1
Then    Left(R, Len(R)-1)
Else    R 

Hope it helps.

Cheers, Pete

### RE: Extracting from string

(OP)
Thanks Pete - Fantastic job as always.. The only thing I noticed in ym brief check is that if the building code is missing (no -) it is printing the whole string. If there is no - , I'd like it to leave the field blank... Thank you!!!

### RE: Extracting from string

In that case, this should work:

#### CODE --> {@RESULT}

WhilePrintingRecords;
Local NumberVar u := UBound(Split({Table.Column}, '-'));
Local NumberVar i;
Local StringVar R;

If      u > 1
Then    (
For i := 1 to (u-1) step 1 do
R := R + Split({Table.Column}, '-')[i] + '-';
);

If      u > 1
Then    Left(R, Len(R)-1)
Else    R 

Cheers
Pete

### RE: Extracting from string

(OP)
Thanks again Pete. At first review, looks great.. Now I get to present this to my users... Thanks!!!!

### RE: Extracting from string

Glad I could help. Please post back if you require anything further.

Regards
Pete

### RE: Extracting from string

(OP)
Actually I am stil having an issue. I have a record where it is Process Eng. PL1 and it is pulling up in the formula as Process Eng . Would like for it to come up blank because of no - . Can you help?
Thanks!!!!

### RE: Extracting from string

Interesting. I just tested that in my test report and the result came up blank as required.

I'm not sure what the problem could be - are you able to upload the report with saved data so I can take a look?

Pete

### RE: Extracting from string

(OP)
Thanks - My mistake - the field actually read Process Eng. -PL1 I didn't notice the - at first. Your formula properly reported everything to the left of the - It was a data issue.. My apologies and thanks again

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!