views:

968

answers:

4

I have column that contains strings. The strings in that column look like this:

FirstString/SecondString/ThirdString

I need to parse this so I have two values:

Value 1: FirstString/SecondString Value 2: ThirdString

I could have actually longer strings but I always nee it seperated like [string1/string2/string3/...][stringN]

What I need to end up with is this:

Column1: [string1/string2/string3/etc....] Column2: [stringN]

I can't find anyway in access to do this. Any suggestions? Do i need regular expressions? If so, is there a way to do this in the query designer?

Update: Both of the expressions give me this error: "The expression you entered contains invalid syntax, or you need to enclose your text data in quotes."

expr1: Left( [Property] , InStrRev( [Property] , "/") - 1), Mid( [Property] , InStrRev( [Property] , "/") + 1)

expr1: mid( [Property] , 1, instr( [Property] , "/", -1)) , mid( [Property] , instr( [Property] , "/", -1)+1, length( [Property] ))
A: 

mid(col, 1, instr(col, "/", -1)) , mid(col, instr(col, "/", -1)+1, length(col))

pappes
Can you give me an idea of what your doing? what does "col" refer to?
Micah
col is supposedly your column.
ΤΖΩΤΖΙΟΥ
SubClassName: mid( [Property] , 1, instr( [Property] , "/", -1)) , mid( [Property] , instr( [Property] , "/", -1)+1, length( [Property] )) says it contains invalid data or needs to close text in quotes.
Micah
@Micah: check my query code in my answer. Also, note that both pappes' and my answer define *two* columns; your comment suggest you copy-paste as an expression for a *single* column.
ΤΖΩΤΖΙΟΥ
That was my problem. However I still couldn't get pappes to work right.
Micah
+1  A: 

In a query, use the following two expressions as columns:

Left(col, InStrRev(col, "/") - 1), Mid(col, InStrRev(col, "/") + 1)

col is your column.

If in VBA, use the following:

last_index= InStrRev(your_string, "/")

first_part= Left$(your_string, last_index - 1)
last_part= Mid$(your_string, last_index + 1)
ΤΖΩΤΖΙΟΥ
This gives me an error with quotes. Is "/" some sort of reserved charater?
Micah
Where do you type the columns and "/" gives you an error? Please update your question with the full SQL statement, in order to give you a more complete reply.
ΤΖΩΤΖΙΟΥ
A: 

Is there any chance you can fix the underlying data structure to be properly normalized so that you can avoid the problem in the first place? Along with retrieving the data comes a whole host or problems with maintaining it accurately, and that would all be ameliorated if you weren't storing multiple values in a single field.

--
David W. Fenton
David Fenton Associates

David-W-Fenton
A: 

I know you're trying to do this inside a query so the SQL string functions are probably your best bet.

However, it's worth mentioning that there's a regular expression COM object accessible from VBA. Just add a reference to the Microsoft VBScript Regular Expressions library inside of your macro code.

Then you can do stuff like this

Dim szLine As String
Dim regex As New RegExp
Dim colregmatch As MatchCollection

With regex
    .MultiLine = False
    .Global = True
    .IgnoreCase = False
End With

szLine = "FirstString/SecondString/ThirdString"

regex.Pattern = "^(.*?\/.*?)/(.*?)$"
Set colregmatch = regex.Execute(szLine)

'FirstString/SecondString
Debug.Print colregmatch.Item(0).submatches.Item(0)
'ThirdString
Debug.Print colregmatch.Item(0).submatches.Item(1)
Mark Biek