Formating field inside mysql query

rafaelrgl
rafaelrgl used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:
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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial