Solved

T-SQL Split name at comma

Posted on 2014-10-06
7
211 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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:
ScottPletcher 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now