Solved

T-SQL Split name at comma

Posted on 2014-10-06
7
213 Views
Last Modified: 2014-10-10
I have seen a lot of questions and answers for this but not a lot for my situation.

I have lastname,firstname<space>(maybe middle initial, maybe full middle name, maybe nothing whatever it is I do not want it)

The convention from the source system is lastname, firstname<space> "the rest" which is enforced at the field level. Sorry it should have been designed differently but was not. I did not do it.

Thanks
0
Comment
Question by:phil916
7 Comments
 
LVL 26

Expert Comment

by:Nick67
ID: 40364650
So, what are you hoping for?
A query that will take the input field and return everything left of the comma as one field and everything between the comma and the space as a second field?
0
 

Author Comment

by:phil916
ID: 40364660
Sally M Smith is entered as

Smith,Sally M

I want
SELECT
Smith
,Sally

Most methods I can find will give me

Smith
,Sally M
0
 

Author Comment

by:phil916
ID: 40364690
I should add that it could be

Smith,Sally M
Jones,Tommy Lee
Gifford,Frank

In all cases I need only first and last separated.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 26

Expert Comment

by:Nick67
ID: 40364717
Patindex is your friend here.
It is constructing RIGHT statement that's a bit tough.
I don't have any data with a comma and no space immediately, but the idea is there

Here's a start.
My table is tblClients and [Client Name]
select [client name],
LEFT([Client Name],PATINDEX ( '%,%' , [Client Name] )-1) as lastname,
LEN([Client Name]) as length,
PATINDEX ( '% %' , [Client Name]) as space,
PATINDEX ( '%,%' , [Client Name] ) as comma,
left([Client Name],PATINDEX ( '% %' , [Client Name])) as firstname
from dbo.tblClients
where [Client Name] like '%,%'

Open in new window


The RIGHT Statement isn't correct yet.
I'll get it in a bit, but give it a whirl
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 200 total points
ID: 40364754
SELECT name AS original_name, LEFT(name, CHARINDEX(' ', name + ' ', CHARINDEX(',', name)) - 1) AS last_and_first_only
FROM (
    SELECT 'Smith,Sally M' AS name UNION ALL
    SELECT 'Jones,Tommy Lee' UNION ALL
    SELECT 'Gifford,Frank'
) AS test_data
0
 
LVL 32

Expert Comment

by:bhess1
ID: 40364886
Try this snippet:
SELECT [All needed fields but name],
	SUBSTRING(BadFieldNoCookie, 1, CHARINDEX(',', BadFieldNoCookie)-1 AS LastName,
	SUBSTRING(SUBSTRING(BadFieldNoCookie, 1, CHARINDEX(' ', BadFieldNoCookie + ' ')-1), CHARINDEX(',', BadFieldNoCookie), LEN(BadFieldNoCookie)) AS FirstName
FROM ATable

Open in new window

Obviously, change the field and table names....
0
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 300 total points
ID: 40364921
Persnicketty bit of syntax.
This breaks your data into separate fields and ditches the comma
Is that what you want?
To break your ugly field into two fields and ditch middle names and initials?
You can do without [name], length, thespace, and comma , but in code it shows you the math

select [name],
LEN([Name]) as length,
PATINDEX ( '% %' , [Name]) as thespace,
PATINDEX ( '%,%' , [Name] ) as comma,

Case PATINDEX ( '% %' , [Name])
	when 0 then RIGHT([name],len([name])- PATINDEX ( '%,%' , [Name] ))
	else RIGHT (LEFT([name],PATINDEX ( '% %' , [Name])), PATINDEX ( '% %' , [Name]) - PATINDEX ( '%,%' , [Name] )) 
end AS firstname,
LEFT([Name],PATINDEX ( '%,%' , [Name] )-1) as lastname
 FROM (
     SELECT 'Smith,Sally M' AS name UNION ALL
     SELECT 'Jones,Tommy Lee' UNION ALL
     SELECT 'Gifford,Frank'
 ) AS test_data 

Open in new window


"Most methods I can find will give me

 Smith
 ,Sally M "

It's taking the right 'x' characters of the left 'y' characters that is the trick to getting rid of what you don't want.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question