?
Solved

T-SQL Split name at comma

Posted on 2014-10-06
7
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 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:Brendt Hess
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 1200 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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