grabbing 6 characters from the middle of a variable length string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Galiphraen
    New Member
    • Mar 2014
    • 3

    #1

    grabbing 6 characters from the middle of a variable length string

    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?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Timelord?

    Try :
    Code:
    Mid(strData, Len(strData) - 6, 6)
    Alternatively, the following will also work :
    Code:
    Left(Right(strData, 7), 6)

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Please post the full error message.

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

      Comment

      • Galiphraen
        New Member
        • Mar 2014
        • 3

        #4
        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.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          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.

          Comment

          Working...