October 15, 2002 at 1:25 pm
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???
October 15, 2002 at 1:57 pm
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
October 16, 2002 at 7:21 am
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
October 16, 2002 at 11:12 am
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
October 18, 2002 at 7:42 am
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.....
October 18, 2002 at 8:19 am
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.
October 18, 2002 at 8:55 am
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.
October 18, 2002 at 9:24 am
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
October 21, 2002 at 9:15 am
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)
October 22, 2002 at 8:04 am
Thank you all, these have been a great help
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply