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)
 Parse out

Author  Topic 

ladiaocb
Yak Posting Veteran

57 Posts

Posted - 2004-07-27 : 11:02:05
I am trying to parse out a name from a row. The data in the row appears as:

Display Name
Lastname,Firstname

Anyone have a query that will parse out the name so that i can extract just the Lastname or just the Firstname?

THANKS!

lee_h
Starting Member

36 Posts

Posted - 2004-07-27 : 11:19:20
Try using substring and charindex:

select substring([Display Name], 1, charindex(',', [Display Name])-1) LastName,
substring([Display Name], charindex(',',[Display Name])+1, Len([Display Name])) FirstName
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-07-27 : 11:38:36
sometimes a subquery makes this more readable (maybe even a little faster):


SELECT substring([Display Name], 1, CommaPosition - 1) LastName,
substring([Display Name], CommaPosition + 1, Len([Display Name])) FirstName
FROM
(select *, charindex(',', [Display Name]) as CommaPosition from YourTable) A


(I hate how substring() makes the 3rd argument required ... it'd be nice to be able to leave it blank and have it take everything remaining after the start position)

- Jeff
Go to Top of Page

ladiaocb
Yak Posting Veteran

57 Posts

Posted - 2004-07-27 : 11:48:48
Thanks for your help. It works!!!!!!
Go to Top of Page
   

- Advertisement -