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

Splitting a column into Fname Lname Expand / Collapse
Author
Message
Posted Tuesday, October 15, 2002 1:25 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 27, 2008 1:10 PM
Points: 43, 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???




Post #7450
Posted Tuesday, October 15, 2002 1:57 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 9:05 AM
Points: 976, Visits: 59
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
Post #43858
Posted Wednesday, October 16, 2002 7:21 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, March 13, 2014 10:40 AM
Points: 572, 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
Post #43859
Posted Wednesday, October 16, 2002 11:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 6, 2002 12:00 AM
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




Post #43860
Posted Friday, October 18, 2002 7:42 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, August 31, 2008 12:56 PM
Points: 281, 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.....




Post #43861
Posted Friday, October 18, 2002 8:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 12:01 AM
Points: 2,720, Visits: 33
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.



Post #43862
Posted Friday, October 18, 2002 8:55 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, August 31, 2008 12:56 PM
Points: 281, 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.








Post #43863
Posted Friday, October 18, 2002 9:24 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 9:05 AM
Points: 976, Visits: 59
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
Post #43864
Posted Monday, October 21, 2002 9:15 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 6, 2014 1:36 PM
Points: 152, 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)
Post #43865
Posted Tuesday, October 22, 2002 8:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 27, 2008 1:10 PM
Points: 43, Visits: 2
Thank you all, these have been a great help




Post #43866
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse