Solved

T-SQL Split name at comma

Posted on 2014-10-06
7
214 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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