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
 Transact-SQL (2000)
 Extracting First and Last Name from a single field

Author  Topic 

tsiegle
Starting Member

9 Posts

Posted - 2003-02-26 : 11:33:32
Can anyone tell me how to extract the first and last name from one single field. i.e. Attorney_tbl.Name = Tracy Siegle and I want to return Lname and Fname.

I can't figure out how to tell it to stop the output at the space. I think I need to use LEN inside the Substring.
This is what I have so far.
SELECT SUBSTRING(fullname,1,?) FROM Attorneys

Thanks so much!

Thanks!

Tracy Siegle

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-02-26 : 11:48:41
SELECT SubString(FullName, 1, CharIndex(' ', FullName) - 1) AS Fname,
SubString(FullName, CharIndex(' ', FullName) + 1, 99) AS Lname
FROM Attorneys


The 99 is just a setting to go to the end of the string. On the off chance that the column allows for more than 99 characters, increase that number to match the max size of the column.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-02-26 : 13:25:31
You could also use reverse to get the last name of the string and that would be independent of the length and also work if there was a middle name.
As this table is named Attorney_tbl I would expect a lot to have middle initials and other strange things.

Could also replace the spaces with full stops and use parsename. Reverse again to get the last name.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -