SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Parsing Last,First Middle Name


Parsing Last,First Middle Name

Author
Message
ajlefort
ajlefort
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 3
Hi,
I need to parse last name,first name space middle name.
Example A - DOE,JOHN
Example B - DOE,JOHN A

I am able to parse the last name.
left(dbo.AbstractData.Name, charindex(',', dbo.AbstractData.Name)-1) as last name

I can parse the first and middle names together.
ltrim(right( dbo.AbstractData.Name,(len(dbo.AbstractData.Name)-charindex(',',dbo.AbstractData.Name))))
as firstmiddlename

I can parse the middle name.
SUBSTRING(dbo.AbstractData.Name,CHARINDEX(' ',dbo.AbstractData.Name + ' ')+1,LEN(dbo.AbstractData.Name))

I am having trouble parsing the first name. The first name is everything after the comma and before the first blank after the comma (or the end of the string, in which case there is no middle name).
Can anyone help me parse the first name only?

Thanks
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61717 Visits: 17954
Not 100% certain what you are after here but take a look at this. This will parse everything into last, first, middle.


if OBJECT_ID('tempdb..#Something') is not null
drop table #Something

create table #Something
(
SomeID int identity,
FullName varchar(25)
)

insert #Something
select 'DOE,JOHN' union all
select 'DOE,JOHN A'

select *
from #Something
cross apply dbo.DelimitedSplit8K(replace(FullName, ' ', ','), ',') p



You can find the code for the DelimitedSplit8K function by following the link in my signature about splitting strings.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41368 Visits: 19815
If you ever cross with a spanish last name with spaces, you might have problems with the 8K splitter.
This formula might have its own problems, but it's another option for you.


if OBJECT_ID('tempdb..#Something') is not null
drop table #Something

create table #Something
(
SomeID int identity,
FullName varchar(25)
)

insert #Something
select 'DOE,JOHN' union all
select 'DOE,JOHN A' union all
select 'DE LA VEGA,ELENA'

SELECT SUBSTRING( FullName, CHARINDEX(',', FullName) + 1, ISNULL(NULLIF(CHARINDEX(' ', FullName, CHARINDEX(',', FullName)), 0), 8000) - (CHARINDEX(',', FullName) + 1))
,CHARINDEX(',', FullName) + 1
,ISNULL(NULLIF(CHARINDEX(' ', FullName, CHARINDEX(',', FullName)), 0), 8000) - (CHARINDEX(',', FullName) + 1)
FROM #Something




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
sqldriver
sqldriver
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2150 Visits: 2536
This isn't precisely what you're trying to do, but I found it helpful when trying to do some similar parsing. There's a lot you have to look out for.

http://www.informit.com/articles/article.aspx?p=25049
Steven Willis
Steven Willis
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1999 Visits: 1721
Take a look at the procedure in this post:

http://www.sqlservercentral.com/Forums/FindPost1472808.aspx


 
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search