Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Parsing the Name Field

Author  Topic 

rkumar28
Starting Member

49 Posts

Posted - 2005-11-21 : 15:55:31
Hello,
I have a scenario where I have a tableA that has a NAME field. I have to grab the first name and last name from this field and insert into another tableB which has First Name and Last Name columns.

Basically I want to take the First Space from the right as last name and rest of them as firstname. Is the a way a substring function can parse the first space from the right.
I want to achive something like below:

TableA has:

NAME FIELD
______
ron decosta
mathews m perry
Biran C Mconnary


I want tableB as

FirstName FIELD .........LastName FIELD
________________ ......._________________

ron ...............decosta
mathew m ...........perry
Brian C .............Mconnary
joe m ...............BOEKE

I have used dots just for formatting purpose.

Will appreciate any advice.

Thanks

Raj

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2005-11-21 : 16:16:27
You need to checkout the string function CHARINDEX and REVERSE in books on line and it will show you the format to find the location of the spaces. (Reverse will return a reverse of the string, and then CHARINDEX will find the first space in it. You can then use RIGHT starting at the LEN - CHARINDEX that you returned.

In your example your expected results violate the question you asked. In that according to your definition the last row should return 111 as the last name, but that is not what you put in your expected output. So I didn't try to put the command together since it wouldn't work for your actual data/illustration of expectations.
Go to Top of Page

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2005-11-21 : 16:17:40
No fair editing it half way through my explantion ... I'll look into it now that you have corrected the data ...
Go to Top of Page

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2005-11-21 : 16:28:07
The following seems to work for me ... give it a shot.
rtrim(convert(char, right(myName, charindex(' ', reverse(myname) )-1 ) )) as LastName,
rtrim(convert(char, substring(myName, 1, len(myname) - charindex(' ', reverse(myname))))) as FirstName
Go to Top of Page

rkumar28
Starting Member

49 Posts

Posted - 2005-11-21 : 16:30:55
quote:
Originally posted by druer

No fair editing it half way through my explantion ... I'll look into it now that you have corrected the data ...



Hi Druer,
Thanks for replying. I am sorry I had to edit the last portion. I removed last name that had 111 in it as I just need the first space from the right as a last name and remaining as first name. Let me try looking online on the functions you mentioned.

Thanks


Raj
Go to Top of Page

rkumar28
Starting Member

49 Posts

Posted - 2005-11-21 : 16:46:27
quote:
Originally posted by druer

The following seems to work for me ... give it a shot.
rtrim(convert(char, right(myName, charindex(' ', reverse(myname) )-1 ) )) as LastName,
rtrim(convert(char, substring(myName, 1, len(myname) - charindex(' ', reverse(myname))))) as FirstName




Thanks Druer..That perfectly worked for me....
Thanks


Raj
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2005-11-21 : 17:00:56
Why are you leaving the middle initial concatenated with the first name? Are you going to store Rebecca De Mornay's first name as "Rebecca De" and her last name as just "Mornay"?
Go to Top of Page
   

- Advertisement -