0

I am trying to remove a ton of unnecessary rows in this table.

Table

Currently there are 1.3m of them which are all an individual transaction id - with the country and destination assigned for each which is wayy wrong, given that the actual flight id is contained within the transaction id string.

The worst thing in the dataset which I've inherited, is the fact that within the transactions table there is a column with the extracted flight id as if it were a foreign key, but above is the 'flights' table. 'eyeroll'

My question is, how do I extract just the part which I have coloured in red? I can't use Left, Mid or Right, because they are variable lengths, some with 3 and 4 digits in the flight id itsself.

Is there a regex or something which can extract the text situated between 2 sets of underscores? which is just about the only viable rule in this sense.

Please bare in mind that I have never used regex before, and would need advice on how to format the expression in my query design view.

Any help is appreciated.

2
  • Do you consistently have a leading length of 1_20120113_ on the front of the txn_id value? If you don't have to figure out the position of the 2nd _, that makes this easier.
    – Dan
    Commented Jan 29, 2016 at 17:10
  • That's right, the length before is always the same, but the length of text I want to extract is variable Commented Jan 29, 2016 at 17:13

1 Answer 1

1

Since your starting position is fixed (after the 2nd _), you can use an Instr() function to find the position of the 3rd _, and just take the characters between.

Instr() returns the postion of the first instance of a character in a string. If the character is not found, it'll return 0. Note, Access considers the first character position to be 1.

Give this a try:

SELECT Mid(txn_id, 12, InStr(12, txn_id, '_')-12) 
FROM MyTable`

Just for reference: https://support.office.com/en-us/article/InStr-Function-85d3392c-3b1c-4232-bb18-77cd0cb8a55b

Edit:

This is pretty gross, but if the 2nd _ position was also variable, this should work:

SELECT Mid(txn_id
           , Instr(Instr(1, txn_id, '_')+1, txn_id, '_')+1
           , Instr(
                   Instr(
                         Instr(1, txn_id, '_')+1
                         , txn_id
                         , '_')+1
                   , txn_id
                   , '_')-1 -  Instr(Instr(1, txn_id, '_')+1, txn_id, '_')
            )
FROM MyTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.