October 27, 2006 at 12:28 pm
HI I have a column ful name in the database and i have to split into three fileds first name,last name and middle name.
FullName:
Johnson, kate L
Boje, timothy W
I need to split the above strings as
Fist_name last_name middle_name
johnson kate L
Boje timothy W
SELECT full_name,SUBSTRING(full_name,1,CHARINDEX(',',full_name)-1) as first_name,
SUBSTRING(LTRIM(RTRIM(SUBSTRING(full_name,CHARINDEX(',',full_name)+1,LEN(full_name)))),1,CHARINDEX('$',REPLACE(LTRIM(RTRIM(SUBSTRING(full_name,CHARINDEX(',',full_name)+1,LEN(full_name)))),' ','$'))) as last_name,
SUBSTRING(REPLACE(LTRIM(RTRIM(SUBSTRING(full_name,CHARINDEX(',',full_name)+1,LEN(full_name)))),' ','&'),CHARINDEX('&',REPLACE(LTRIM(RTRIM(SUBSTRING(full_name,CHARINDEX(',',full_name)+1,LEN(full_name)))),' ','&'))+1,LEN(REPLACE(LTRIM(RTRIM(SUBSTRING(full_name,CHARINDEX(',',full_name)+1,LEN(full_name)))),' ','&'))) AS middle_name
FROM names
I am using the above script to do this.
But is there any better way of achieving this?
Thanks.
October 27, 2006 at 5:52 pm
Is first name always guaranteed to be there? If not, what happens to the comma? Is Middle Initial always guaranteed to be there? Can the last name have a space in it as in St. Jean, etc?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 30, 2006 at 7:26 am
Not withstanding Jeff's comments
if all the names are the same format
PARSENAME(REPLACE(REPLACE(full_name,',',''),' ','.'),2) AS [First_Name],
PARSENAME(REPLACE(REPLACE(full_name,',',''),' ','.'),1) AS [Middle_Name],
PARSENAME(REPLACE(REPLACE(full_name,',',''),' ','.'),3) AS [Last_Name]
Far away is close at hand in the images of elsewhere.
Anon.
October 30, 2006 at 2:43 pm
Did you hear such name as "Jean-Claude Van Damme"?
Try to work it out.
After you finish with it start with his original name: "Jean-Claude Camille François Van Varenberg"
_____________
Code for TallyGenerator
October 30, 2006 at 2:47 pm
LMAOROTF.
Seriously, this is the kind of process you automate as much as possible, but in the end you always have to manually validate the results.
October 30, 2006 at 4:41 pm
>>After you finish with it start with his original name: "Jean-Claude Camille François Van Varenberg"
Leave it to Serqiy to know something like that But, both he and Remi are absolutely correct.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 30, 2006 at 4:47 pm
If every name were:
lastname comma space firstname space initial.
All you would need to do is create a variable @thename:
1. put the fullname in there
2. copy all upto the comma into @lastname
3. put the left over into the @thename
4. reverse the string and count the characters upto the first space. If it's equal to 1, put it into @midinitial,
5. take whatever is left over and put it into @firstname
6. use @firstname, @midinitial, @lastname however you need.
-SQLBill
October 30, 2006 at 6:07 pm
Jeff,
I did not ask about born name of Pele.
Bill,
All he needs to do is:
1) store all parts of names in database separately;
2) store all parts of names in database separately;
3) store all parts of names in database separately;
4) store all parts of names in database separately;
5) store all parts of names in database separately;
6) use ID instead of FullName to reference the person in all tables but "Person".
_____________
Code for TallyGenerator
October 31, 2006 at 8:59 pm
Heh... Pele, huh?
Anyway, I agree... if you are in control of the source of the data, then "store all parts of names in databases separately". However, the story changes a bit when some 3rd party sends you data in a "less than optimal format" like the name examples in this thread.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 31, 2006 at 9:32 pm
Does not matter.
Whatever whoever sends to you turn it into actual data when INSERT, not when SELECT.
If you (with your code) cannot understand how to convert this s..t they sent into data report validation error and request properly formatted values.
_____________
Code for TallyGenerator
October 31, 2006 at 10:12 pm
Sure, sure.... but we both know that they don't always comply... yep, it's crap but sometimes (like in the telephone reseller business) they send you crap just to make your life difficult
--Jeff Moden
Change is inevitable... Change for the better is not.
November 1, 2006 at 3:25 am
Don't tell me about what they send.
I deal with it every day.
But it does not mean I let this mess into my database. If I cannot work it out during the import nothing is coming in until the issue is sorted out.
And believe me, it works.
_____________
Code for TallyGenerator
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply