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 2008 Forums
 Transact-SQL (2008)
 parse string to comma

Author  Topic 

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-02-18 : 10:28:25
i have a field called name, it is the last name and first name seperated by a comma, i would like to parse the last name prior to the comma, how do i do that please?
thank you

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-18 : 10:33:17
SELECT LEFT(Name, CHARINDEX(',', Name)-1) FROM myTable
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-02-18 : 10:43:46
[code]
DECLARE @name varchar(100); SET @name = 'Kaiser,Brett'
SELECT '"' +SUBSTRING(@name,1,CHARINDEX(',',@name)-1)+'"'
AS LastName
, '"'+SUBSTRING(@name,CHARINDEX(',',@name)+1,LEN(@name)-LEN(SUBSTRING(@name,1,CHARINDEX(',',@name)-1)))+'"'
AS FirstName

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-02-18 : 10:53:28
thank you
i used to get first and last.
LEFT(dbo.CV3CareProvider.DisplayName, CHARINDEX(',', dbo.CV3CareProvider.DisplayName)-1),
SUBSTRING(dbo.CV3CareProvider.DisplayName,CHARINDEX(',',dbo.CV3CareProvider.DisplayName)+1,LEN(dbo.CV3CareProvider.DisplayName)-LEN(SUBSTRING(dbo.CV3CareProvider.DisplayName,1,CHARINDEX(',',dbo.CV3CareProvider.DisplayName)-1))),
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-18 : 12:02:17
or

LEFT(DisplayName, CHARINDEX(',', DisplayName)-1),
RIGHT(DisplayName, CHARINDEX(',', reverse(DisplayName))-1)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -