Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Breaking up a string of text Expand / Collapse
Author
Message
Posted Tuesday, September 24, 2013 12:20 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 25, 2014 11:46 AM
Points: 98, Visits: 241
Hello again,

I have some very dirty data here. I'm working on a Customers table which I plan to clean up and insert into a new table. Currently, the table looks like this:

LastName, FirstName, MiddleInt
-----------------------------------------
Smith,John,Q * *

So "Smith,John,Q" IS the last name and "*" are stored in the First Name and Middle Int fields.

Yuk.

I am able to just select the last name based on the comma like so:

LEFT(LastName,(CHARINDEX(',',LastName))) AS LastNameOnly

However, I am having trouble pulling out the first name. Hell, at this point, I'd be fine with it if first name contained any possible Middle Int. I'd be fine if FirstName was "John,Q". I can clean that up in a second step.

I'd be grateful if someone could help just getting the "First Name" out of the string. I've tried various methods of SUBSTRING, LEN, and what not. Just can't seem to connect the dots....

Thanks




Post #1498006
Posted Tuesday, September 24, 2013 12:41 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:26 PM
Points: 597, Visits: 7,195
This article should be handy:

DelimitedSplit8K

Create the DelimitedSplit8K function as detailed in the article, and CROSS APPLY it to your comma-delimited data, like so:

SELECT * FROM YourTable
CROSS APPLY YourDatabase.YourSchema.DelimitedSplit8K(YourTable.Lastname,',')

Replace the "Your" bits with your actual tables. This will show you how the original string, and how it will be split out based on the delimiter (in this case, the comma at the end of the DelimitedSplit8K call). From there, you'll need to update each part of the name with the split-out entries you end up with from using DelimitedSplit8K. This can be clarified further as needed, but it should provide a good starting point.





-
Post #1498015
Posted Tuesday, September 24, 2013 12:43 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 25, 2014 11:46 AM
Points: 98, Visits: 241
This seems to mostly work:

RIGHT(LastName,(LEN(LastName))-CHARINDEX(',',LastName)) As FirstNameOnly

I needed to use Right Trim but I didn't know how many positions to trim to. So, I did this:

Right(LastName, C)

A = Length of whole string
B = Position of Comma
C = A-B (Remaining chars)


Post #1498017
Posted Tuesday, September 24, 2013 2:52 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 12:52 PM
Points: 111, Visits: 343
try

select

substring( lastname, charindex(',', lastName )+1,
(charindex (',', lastName, charindex(',', lastName )+1 )-
charindex(',', lastName ))-1
)
Post #1498045
Posted Tuesday, September 24, 2013 11:48 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 9:53 PM
Points: 3,438, Visits: 5,390
My advice is plan for the worst and use a pattern split function like the one you'll find in the 4th link in my signature.

WITH SampleData (Name) AS
(
SELECT 'Smith,John,Q'
UNION ALL SELECT 'Fedders-Smith,John,Q'
UNION ALL SELECT 'Smith, John Q'
UNION ALL SELECT 'Smith,John Quincy'
UNION ALL SELECT ' Smith,John Quincy'
)
SELECT Name
,LastName=MAX(CASE ItemNumber WHEN 1 THEN Item END)
,FirstName=MAX(CASE ItemNumber WHEN 3 THEN Item END)
,LastName=MAX(LEFT(CASE ItemNumber WHEN 5 THEN Item END, 1))
FROM
(
SELECT Name, ItemNumber, Item
FROM SampleData a
CROSS APPLY dbo.PatternSplitCM(LTRIM(Name), '[\-a-zA-Z]') b
WHERE [Matched]=1
) a
GROUP BY Name;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1498129
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse