July 1, 2009 at 9:22 am
I need to find the first and last names (separately).
I am using:
Left(Job_Descript.PM,CHARINDEX(' ',Job_Descript.PM) - 1)
for the first name.
Is there a change to use CHARINDEX from the right.
Something to consider is that there is occasionally middle initials. If there wasn't I could simply start with the space and subtract from the LEN. But... the middle initial stops that idea.
Thank you,
July 1, 2009 at 9:33 am
do you want something like this?
declare @nam varchar(30)
set @nam='pradeep singh'
select left(@nam, charindex(' ',@nam)-1)
select right(@nam, len(@nam)-charindex(' ',@nam))
OUTPUT
------------
------------------------------
pradeep
(1 row(s) affected)
------------------------------
singh
(1 row(s) affected)
July 1, 2009 at 9:36 am
Is that going to work with the name: John Q. Public to give me:
First Name = 'John'
Last Name = 'Public'
July 1, 2009 at 9:37 am
Have you tried this.
taking the above post as base code.
REVERSE(left(REVERSE(@nam), charindex(' ',REVERSE(@nam))-1))
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
July 1, 2009 at 9:40 am
you could also use this for the surname:
SUBSTRING(@nam,LEN(@nam) - charindex(' ',@nam),LEN(@nam))
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
July 1, 2009 at 11:15 am
I never thought of using a 'double reverse'
Thank you very much, that was great and works no problem.
July 1, 2009 at 2:49 pm
REVERSE is one of the less performant string functions in TSQL, so this alternative expression calls REVERSE only once.
SELECT RIGHT(@nam, CHARINDEX(' ', REVERSE(@nam)) - 1)
The expression above and also some of those in earlier posts will throw an error if the value of @nam has no spaces. If this is a possibility, you may want to use one of these more complex expression:
/* Returns entire string if there are no spaces */
SELECT RIGHT(@nam, COALESCE(NULLIF(CHARINDEX(' ', REVERSE(@nam)), 0) - 1, LEN(@nam)))
/* Returns empty string if there are no spaces */
SELECT RIGHT(@nam, COALESCE(NULLIF(CHARINDEX(' ', REVERSE(@nam)), 0) - 1, 0))
July 2, 2009 at 7:55 am
It is going to take me a bit to figure why/how the Coalese works with the reverse, etc..
In the meantime since it saves on performance I have switched to it. Now comes the task of interpreting it.
I appreciate your help,
Thank you,
July 2, 2009 at 11:09 am
REVERSE is a fairly expensive function performance wise. No need to use three of them just to get the last name. I'll be back in a minute...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2009 at 11:10 am
Jeff Moden (7/2/2009)
REVERSE is a fairly expensive function performance wise. No need to use three of them just to get the last name. I'll be back in a minute...
Heh... I should read the whole post before I post... Andrew already did exactly what I would do... well, except on Tuesdays. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 5, 2009 at 9:29 pm
Jeff Moden (7/2/2009) Andrew already did exactly what I would do...
I would not.
There was a member of Led Zeppelin, John Paul Jones.
Would not be happy seeing his name after such "parsing".
_____________
Code for TallyGenerator
July 6, 2009 at 8:35 am
Sorry, did the same as Jeff, didn't read Andrews answer and posted the same solution. :hehe:
July 6, 2009 at 8:38 am
...twice !! dozy ******
July 6, 2009 at 10:12 am
andrewd.smith (7/1/2009)
REVERSE is one of the less performant string functions in TSQL, so this alternative expression calls REVERSE only once.
SELECT RIGHT(@nam, CHARINDEX(' ', REVERSE(@nam)) - 1)
Bear in mind that this only returns the last name if the last name contains no spaces. My last name consists of 2 words separated by a space and it is parsing routines such as this that send me mail to the last half of my name or file me under the wrong letter. Of course the real problem is the that the name is not stored in separate fields to begin with.
Terri
To speak algebraically, Mr. M. is execrable, but Mr. C. is
(x+1)-ecrable.
Edgar Allan Poe
[Discussing fellow writers Cornelius Mathews and William Ellery Channing.]
July 6, 2009 at 12:10 pm
Of course the real problem is the that the name is not stored in separate fields to begin with.
Agreed.
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply