Formating field inside mysql query

Hi, I have one field called seller, and inside I have those kind of values

JAMROZ,STOMP F & MARIA N
JOHN,FORD F
MARY, STEMP
PAUL, FORD & MARY N

So as you can see I have first name, last name ( maybe middle name), than i have & if there is another person, and aftert that First Name second person and Middle name last person. I need a way to extract those inside my query so i have something like this


select (conditions to get this) as Seller1First,  (conditions to get this) as Seller1Middle,  (conditions to get this) as  Seller1Last,  (conditions to get this) as Seller2First, Seller2Last from .......

This is MYSQL, but I added the sql topics too since is related.
LVL 1
rafaelrglAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
I think this should do it:
SELECT seller
  , TRIM(Seller1Last) AS Seller1Last
  , TRIM(Seller1First) AS Seller1First
  , TRIM(Seller1Mid) AS Seller1Mid
  , TRIM(Seller2Last) AS Seller2Last
  , TRIM(Seller2First) AS Seller2First
  , TRIM(Seller2Mid) AS Seller2Mid
FROM (
SELECT seller
  , @comma := LOCATE(',', seller), @amp := LOCATE('&', seller)
  , @s1l := CASE WHEN @comma>0 THEN SUBSTR(seller, 1, @comma-1) END AS Seller1Last
  , CASE WHEN @amp>0 THEN 
         CASE WHEN LOCATE(' ', seller, @comma+2)>0 THEN SUBSTR(seller, @comma+1, LOCATE(' ', seller, @comma+2)-@comma) ELSE SUBSTR(seller, @comma+1, @amp-@comma-1) END 
    ELSE 
         CASE WHEN LOCATE(' ', seller, @comma+2)>0 THEN SUBSTR(seller, @comma+1, LOCATE(' ', seller, @comma+2)-@comma) ELSE SUBSTR(seller, @comma+1) END 
    END AS Seller1First
  , CASE WHEN @amp>0 THEN 
         CASE WHEN LOCATE(' ', seller, @comma+2)>0 THEN SUBSTR(seller, LOCATE(' ', seller, @comma+2)+1, @amp-LOCATE(' ', seller, @comma+2)-1) END 
    ELSE 
         CASE WHEN LOCATE(' ', seller, @comma+2)>0 THEN SUBSTR(seller, LOCATE(' ', seller, @comma+2)+1) END 
    END AS Seller1Mid
  , CASE WHEN @amp>0 THEN @s1l END AS Seller2Last
  , CASE WHEN @amp>0 THEN CASE WHEN LOCATE(' ', seller, @amp+2)>0 THEN SUBSTR(seller, @amp+1, LOCATE(' ', seller, @amp+2)-@amp-1) ELSE SUBSTR(seller, @amp+1) END END AS Seller2First
  , CASE WHEN @amp>0 THEN CASE WHEN LOCATE(' ', seller, @amp+2)>0 THEN SUBSTR(seller, LOCATE(' ', seller, @amp+2)+1, LOCATE(' ', seller, @amp+2)-@amp-1) END END AS Seller2Mid
FROM Table1) a

Open in new window

Results:
|                   seller | Seller1Last | Seller1First | Seller1Mid | Seller2Last | Seller2First | Seller2Mid |
|--------------------------|-------------|--------------|------------|-------------|--------------|------------|
| JAMROZ,STOMP F & MARIA N |      JAMROZ |        STOMP |          F |      JAMROZ |        MARIA |          N |
|         SMITH,ANDREW&SAM |       SMITH |       ANDREW |     (null) |       SMITH |          SAM |     (null) |
|              JOHN,FORD F |        JOHN |         FORD |          F |      (null) |       (null) |     (null) |
|              MARY, STEMP |        MARY |        STEMP |     (null) |      (null) |       (null) |     (null) |
|      PAUL, FORD & MARY N |        PAUL |         FORD |            |        PAUL |         MARY |          N |
|     MARY, STEMP G & ANDY |        MARY |        STEMP |          G |        MARY |         ANDY |     (null) |

Open in new window

SQL Fiddle
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rafaelrglAuthor Commented:
Thanks for your help. I have award the points because this is first part of that question. I have created another question with more 2 3 rules. I also update the sample, because is last, first second name format, and on the first question i put first, last sec. here is the link for second question: https://www.experts-exchange.com/questions/29011241/Formating-field-in-mysql-Advance-formatting.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.