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
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 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
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2893 Visits: 113
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
Billing and OSS Specialist - SQL Programmer
MCL Systems
Simon Sabin
Simon Sabin
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1810 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
SSChasing Mays
SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)

Group: General Forum Members
Points: 605 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
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4124 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
SSChasing Mays
SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)

Group: General Forum Members
Points: 605 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
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2893 Visits: 113
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
Billing and OSS Specialist - SQL Programmer
MCL Systems
BrenBart
BrenBart
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 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
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 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