Ask Experts Questions for FREE Help !
Ask
    Jovacle's Avatar
    Jovacle Posts: 5, Reputation: 1
    New Member
     
    #1

    Nov 11, 2008, 10:35 PM
    SQL Expression to extract part of a string divided by dashes
    Hi,

    I have a field (Tag_Num) in an Access database that contains a heap of values that look something like this;

    12345-1234-ABC-40-CG1
    12345-2868-ACC-40-CG1
    12345-4897-ACC-40-CG1
    12345-7874-BGC-40-CG1
    12345-8756-BXC-40-CG1
    12348-123-ABC-40-CG1
    12348-257-HJG-40-CG1
    12348-389-UIO-40-CG1
    12348-487--40-CG1
    12348-575-YUO-40-CG1

    I want to be able to extract all the five sections separated by dashes (-) into separate columns. So far I have been trying to

    do this using the Mid function which has only worked on the first few numbers since they are all only 5 digits long. For

    example;

    SELECT Mid([Tag_Num],1,5) AS Area1
    FROM [table_name]
    ORDER BY Mid([Tag_Num],1,5);

    This will give me the following results;

    Area1

    12345
    12345
    12345
    12345
    12345
    12348
    12348
    12348
    12348
    12348

    If I try and do the same to extract the next few numbers after the fist dash I use this SQL code;

    SELECT Mid([Tag_Num],7,3) AS Area2
    FROM [table_name]
    ORDER BY Mid([Tag_Num],7,3);

    This will give me the following results;

    Area2

    1234
    2868
    4897
    7874
    8756
    123-
    257-
    389-
    487-
    575-

    The problem is obviously that it gets the dashes in. A similar thing occurs for the next few columns.

    Any ideas how I can separate my Tag_Num column into 5 separate columns using the dash (-) as a divider?

    :confused::eek::cool::confused:
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

    Nov 12, 2008, 07:35 AM

    First this is not a SQL expression, its using Access built in functions.

    The thing you are missing is the InStr function. For example, the first part could be parsed out using:

    =Left(fieldname,Instr(1,fieldname."-")-1)

    If the last part is always three characters, I would use:

    =Right(fieldname,3)

    Otherwise use:

    Right(fieldname,Len(fieldname)-InstrRev(fieldname,"-"))

    For the 2nd through 4th parts, you have to use the Mid function. For example:

    Mid(fieldname",Instr(1,fieldname,",Instr(1,fieldna me,")+1,Instr(Instr(1,fieldname,")+1,Instr(Instr(1 ,fieldname,")+1,fieldname,")+1,fieldname,")-Instr(1,fieldname,")-Instr(1,fieldname,")-1)

    The idea is to use the Instr function to determine what the start and length should be for the MID function.
    Jovacle's Avatar
    Jovacle Posts: 5, Reputation: 1
    New Member
     
    #3

    Nov 16, 2008, 07:38 PM

    Man that's brilliant. The Mid example works spot on as is! I've just got to try & figure out how it works now so I can alter it for the next section after the second dash (-). Too good. Very happy.
    Jovacle's Avatar
    Jovacle Posts: 5, Reputation: 1
    New Member
     
    #4

    Nov 16, 2008, 11:33 PM

    The mid example works perfect for values after the first dash, but I still haven't figured out how to alter it to work for the next section after the second dash.

    The best I've done is to use;

    Mid(fieldname,Instr(8,fieldname,"-")+1,Instr(Instr(8,fieldname,"-")+1,fieldname,"-")-Instr(8,fieldname,"-")-1)

    So really I'm just starting it a bit further in (8 is the only change) which is risky considering I could have a value like;

    123-02-ACC-40-CG1

    The above example would end up finding the value after the third dash (40) which is not what I'd be after.

    Also, the above string fails if I have the following possible value;

    12345-2868-ACC

    It won't work if there's no third dash and just reports "#error."

    If this above example had another dash e.g.

    12345-2868-ACC-XX

    then it would work.
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #5

    Nov 17, 2008, 09:59 AM

    Instead of the 8 you need to use another Instr that gets you past the first dash.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How are estate assets divided? [ 2 Answers ]

I am the executor of my uncles estate. His wife wanted nothing to do with his cremation, she didn't want his wedding ring or the remains. He named me as sole heir in his will but by Nh law the spouse gets things like vehicles and real estate. My question is will I get reimbursed for the money that...

3 divided into 3.9 long division [ 1 Answers ]

3 divided into 3.9 Long division

Divided attention [ 3 Answers ]

An example of divided attention is driving while speaking to someone on a cell phone. Another real life, everyday example of divided attention would be?

Math 2/3 divided by 4/25 [ 1 Answers ]

I can't figure it out. 2/3 divided by 4/25


View more questions Search