Solved

T-SQL Split name at comma

Posted on 2014-10-06
7
215 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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