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


Break up full name col into fname, lname cols


Break up full name col into fname, lname cols

Author
Message
scotttr
scotttr
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 1
I have a NAME field that consistes of a person's full name (first, middle, last) separated by spaces. I want to separate the NAME field into FNAME, LNAME, and MNAME fields. My problem is that the names are of variable lengths so I can't use the right, left, or substring functions. I know there is a way of doing it but I haven't figured it out yet. Thanks for the help.



Frank Kalis
Frank Kalis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19503 Visits: 289
Can you post some sample data?

Frank
http://www.insidesql.de
http://www.familienzirkus.de

--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
scotttr
scotttr
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 1
Sure, below is the field heading and some sample data.

INDNAME
----------------------
ROSE DONALD BRUCE
GAMACHE RICHARD HERVE
CARLSON DAVID ROBERT
HOWARD GARY WAYNE
GARNETT FREDERICK H



Frank Kalis
Frank Kalis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19503 Visits: 289
Should not be too difficult if there is always a blank as delimiter.

http://www.sqlservercentral.com/forum/link.asp?TOPIC_ID=17517 might give you some ideas

Frank
http://www.insidesql.de
http://www.familienzirkus.de

--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
Jonathan
Jonathan
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2775 Visits: 54
This may seem easy, but is rife with potential problems. My last name is two words...

If all your data is indeed three strings delimited by single spaces, you could just try something like this:

UPDATE YourTable
SET LName = PARSENAME(REPLACE(LTRIM(RTRIM(IndName)),' ','.'),3),
FName = PARSENAME(REPLACE(LTRIM(RTRIM(IndName)),' ','.'),2),
MName = PARSENAME(REPLACE(LTRIM(RTRIM(IndName)),' ','.'),1)


--Jonathan



--Jonathan
Frank Kalis
Frank Kalis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19503 Visits: 289
quote:

This may seem easy, but is rife with potential problems. My last name is two words...


...and to complicate it a little bit, I guess you don't have a middle name, right?

Anyway, the original error was to store these informations in a single column. And, of course, it will leave always some rest of work for you to do, but it's easier once the data is in separate columns.

Frank
http://www.insidesql.de
http://www.familienzirkus.de

--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
Frank Kalis
Frank Kalis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19503 Visits: 289
...btw, sorry to the original poster!

I wasn't aware of the PARSENAME function.
Sounds really handy for such situations.

Frank
http://www.insidesql.de
http://www.familienzirkus.de

--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
Jonathan
Jonathan
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2775 Visits: 54
quote:

...btw, sorry to the original poster!

I wasn't aware of the PARSENAME function.
Sounds really handy for such situations.

Frank
http://www.insidesql.de
http://www.familienzirkus.de



Yes. I was surprised to see a script someone had posted for a function to split up ip addresses stored as strings; PARSENAME makes that rather easy.

--Jonathan



--Jonathan
Richard Norris
Richard Norris
SSC Veteran
SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

Group: General Forum Members
Points: 292 Visits: 190
This might be useful...

declare @fullname varchar(150),
@space1 int,
@space2 int,
@first varchar(50),
@middle varchar(50),
@last varchar(50)

set @fullname = 'first last'

select @space1 = CHARINDEX(' ',@fullname)
select @space2 = CHARINDEX(' ',@fullname,@space1+1)

select @first = left(@fullname,@space1-1)
if @space2 > 0
BEGIN
select @last = right(@fullname,len(@fullname)-@space2)
select @middle = substring(@fullname,@space1+1,@space2-@space1-1)
END
ELSE
BEGIN
select @last = right(@fullname,len(@fullname)-@space1)
select @middle = ''
END
SELECT @first as FirstName,
@middle as MiddleName,
@last as LastName



John Campbell-96530
John Campbell-96530
SSC-Enthusiastic
SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)

Group: General Forum Members
Points: 124 Visits: 1
Looking up parsename in help doesn't look like it applies to this topic. It looks like it is more concerned with breaking up a multipart server object name i.e. server.database.owner.table.column...



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