Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Parsing Last,First Middle Name Expand / Collapse
Author
Message
Posted Monday, July 15, 2013 12:30 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 15, 2013 1:18 PM
Points: 3, 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
Post #1473843
Posted Monday, July 15, 2013 12:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's 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)
Post #1473855
Posted Monday, July 15, 2013 1:37 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:47 AM
Points: 3,513, Visits: 7,566
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1473872
Posted Tuesday, July 16, 2013 9:48 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 12:36 PM
Points: 412, Visits: 1,631
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
Post #1474198
Posted Tuesday, July 16, 2013 10:10 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Take a look at the procedure in this post:

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


 
Post #1474209
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse