1

I have a problem in parsing my last name and first name in a table. My code:

SELECT value, 
       substr(value,instr(value,' ',1),instr(value,' ',2)-2) last_name,
  FROM (SELECT 'Matt Ryan, QB' value 
          FROM dual);

If I write 'Andrew Luck, QB' it parses properly the last name. But when I write 'Matt Ryan, QB' it parses just two letters from the last name. I identify it by white space. I don't understand what can be wrong here.

'Matt Ryan, QB'
'Andrew Luck, QB'
5
  • what is the problem!?? what do you want from the query? Commented Mar 27, 2015 at 21:43
  • @Farhęg it won't be a wild guess to assume he wants to extract the last name ;) Commented Mar 27, 2015 at 23:42
  • @alfasin my comment was before any edit on question Commented Mar 28, 2015 at 0:36
  • @Farhęg in that case, my apologize! I arrived after it was already clear. Commented Mar 28, 2015 at 0:49
  • @alfasin, no problem that's fine never mind. Commented Mar 28, 2015 at 1:06

3 Answers 3

1

Please consider using regular expression. I think this should solve your problem.

SELECT value, 
    REGEXP_SUBSTR(value,
                '\w+\s+(\w+\s+\w+|\w+),',1,1,NULL,1) last_name
    FROM ( 
    SELECT 'Matt Ryan, QB' value FROM dual 
    );

Check this fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

Jack, If this works for you. Can you please accept my answer.
This seems to be the best answer, I checked all with samples from hr.employees (Oracle HR schema). But something is wrong with 'Lex De Haan, QA' ;-)
I just updated the Reg Exp. This should work for 'Lex De Haan, QA' as well. Please refer to the fiddle.
For me it looks OK, already upvoted your answer, but I'm not Original Poster, he's the judge here;-)
Thanks a lot , sorry for the delay .
1

You're misusing the index parameter that you're passing to instr, plus you should remove the comma that comes after "last_name". Try:

SELECT value, 
substr(value, instr(value,' ')+1, instr(value,' ',instr(value,' '))-1) last_name
FROM ( 
  SELECT 'Matt Ryan, QB' value FROM dual 
);

Link to fiddle

1 Comment

Or sorry this stil doesnt work for me . But reg_exp work perfec.
1

The occurrence parameter of INSTR is the fourth parameter, so you have to use

instr(value,' ', 1, 1) -- first space

and

instr(value,' ', 1, 2) -- seconds space

The third parameter of SUBSTR is the number of characters to extract. This is the position of the second space minus the position of the first space - minus one for the comma:

instr(value,' ', 1, 2) -instr(value,' ', 1, 1) -1

So all in all it's:

SELECT value,
      substr(value, instr(value,' ', 1, 1),
                    instr(value,' ', 1, 2) -instr(value,' ', 1, 1) -1) last_name
 FROM (SELECT 'Matt Ryan, QB' value 
         FROM dual);

Check this Fiddle here.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.