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


Splitting a column into Fname Lname


Splitting a column into Fname Lname

Author
Message
cedar72
cedar72
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 2
In our DB we have a column on the users table of name which stores the users full name (I know, I didn't do it). I need to extract the first names and last names of the users, these are seperated by spaces...I need to do this in the select if possible. Any ideas???



Robert W Marda
Robert W Marda
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1418 Visits: 82
Try this:

SELECT LEFT(fullname,CHARINDEX(' ', fullname, 1) - 1) AS fname,
SUBSTRING(fullname,CHARINDEX(' ', fullname, 1) + 1, LEN(fullname)) AS Lname
FROM tablename

Robert W. Marda
SQL Programmer
bigdough.com
The world’s leading capital markets contact database and software platform.



Robert W. Marda
SQL Programmer
Ipreo

Simon Sabin
Simon Sabin
SSC Eights!
SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)SSC Eights! (800 reputation)

Group: General Forum Members
Points: 800 Visits: 107
The neatest is to use a function that splits the string. (i.e like the split function in VB) Such a function can be used in lots of places in an app i.e dynamic IN clauses.

There are some examples in the script library my script (yet to be approved can be got from http://www.sqlservercentral.com/scripts/scriptdetails.asp?scriptid=528)

Simon Sabin
Co-author of SQL Server 2000 XML Distilled
http://www.amazon.co.uk/exec/obidos/ASIN/1904347088


Simon Sabin
SQL Server MVP

http://sqlblogcasts.com/blogs/simons
Vitor.Ferreira
Vitor.Ferreira
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 1
rmarda code works fine if you just have first and last name. If you a complete name you need something like this

SELECT LEFT(fullname,CHARINDEX(' ', fullname, 1) - 1) AS fname,
right(fullname,CHARINDEX(' ', REVERSE(fullname), 1) - 1) AS Lname
FROM tablename



well0549
well0549
Old Hand
Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)

Group: General Forum Members
Points: 349 Visits: 2
In my humble opinion this will never work.

Let's say somebody is called "Gert Jan Van den Berg"
In this case hist first name is "Gert Jan" and his last name is "Van den Berg"

I don't see how a routine that can split this up in a query.......

Look for another solution. For example create a table with Fullname, first name and last name and then do some joining...... You could even get the "Van den" correct then....

I think this is the only doable solution.....



NPeeters
NPeeters
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3238 Visits: 36
I'm afraid I have to disagree with the post from well0549 to some extent.

You'll never catch all possible situations using any automated algorithm, but if you could convert most of them without or errors, you're already halfway.

But I do like the idea of setting up an additional table, or adding some fields to the table, containing just the first and last name. This way, you could at least do some manual corrections and store them for future use.



well0549
well0549
Old Hand
Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)

Group: General Forum Members
Points: 349 Visits: 2
Npeeters,

My Dad always said, a lifetime almost is a lifetime nothing....

Meaning if it doesn't work vor every situation it actually doesn't work at all.



Robert W Marda
Robert W Marda
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1418 Visits: 82
To be able to accurately separate first names and last names when there are more than one first name and more than one last name you would need some kind of look up list to use to ensure you got them all separated correctly. If there are only one or two names like this then the time it would take to develop this kind of solution would not be worth it when you can quickly fix those two names manually.

Also, If the names stored in fullname only have a first name and a last name the the solutions proposed here will work fine for the problem cited by cedar72.

Robert W. Marda
SQL Programmer
bigdough.com
The world’s leading capital markets contact database and software platform.



Robert W. Marda
SQL Programmer
Ipreo

BrenBart
BrenBart
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 61
I rather like SimonSabin's approach.

It would allow you to handle more special cases such as multiple spaces, single names and counting the number of tokens to determine if their number is what you are expecting. It could either fix these issues on the fly or flag those particular records as a problem.

Also, I usually like to do an LTRIM(RTRIM()) on the full name to clear out any leading/trailing spaces before any other processing occurs. Seems to me when I've done this before I've included a routine to convert any multiple spaces to single spaces. (Recursively converting two spaces to one until no replacements are made.)

With this type of data scrubbing it's rare that you can handle all the special cases. I try to catch what I can and then keep in mind that even if I miss some of them it's far more accurate than before I started. Besides, there's always some salesperson/marketingish person who will come along later and point out your failings.


"I met Larry Niven at ConClave 27...AND I fixed his computer. How cool is that?"
(Memoirs of a geek)

cedar72
cedar72
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 2
Thank you all, these have been a great help



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