By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

grabbing 6 characters from the middle of a variable length string

P: 3
I need to grab the 6 numbers from the middle of a string like ABCDEF123456A and ABCDEFGH123456A. So I want 6 characters starting at the 7 last character in a string. I tried mid(TEXT, len(TEXT)-7,6) but I get a proceedural error.
mid(TEXT,7,6) has no issues but the starting point varies and the mid() does not seem to like the LEN(TEXT)-7 in the middle.
Is there a better way?
Mar 5 '14 #1

✓ answered by Rabbit

Please post the full error message.

I suspect you have a string less than 8 characters long and len - 7 is 0 or negative.

Share this Question
Share on Google+
4 Replies


NeoPa
Expert Mod 15k+
P: 31,494
Timelord?

Try :
Expand|Select|Wrap|Line Numbers
  1. Mid(strData, Len(strData) - 6, 6)
Alternatively, the following will also work :
Expand|Select|Wrap|Line Numbers
  1. Left(Right(strData, 7), 6)
Mar 5 '14 #2

Rabbit
Expert Mod 10K+
P: 12,366
Please post the full error message.

I suspect you have a string less than 8 characters long and len - 7 is 0 or negative.
Mar 5 '14 #3

P: 3
Thanks Rabbit, you are right. There are shorter strings. Focussed on the problem at hand and not the whole picture. :-)
Thanks NeoPa. I will try your suggestion.
It looks like I will need to set up some kind of conditional test to deal with different strings as some look like AB123456 and some like ABCDE123456A.
Mar 6 '14 #4

NeoPa
Expert Mod 15k+
P: 31,494
The point (one of them at least) I was trying to get across is that you don't want Len() - 7 at all in the scenario you described, but Len() - 6.

If you use either of the code snippets I suggested and the data is formatted as you described it in your first post then it will work perfectly for you.
Mar 6 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.