January 20, 2017 at 12:34 pm
I have a Name column that may or may not have Second Consumer and if it does, it may or may not have Middle name.
I need to extract the second consumer First Name, Middle Name (if exists) and Last Name. The first and second consumer is connected by 'AND'
CREATE TABLE #Name (Name VARCHAR(250),
NameCTR VARCHAR(4))
INSERT INTO #Name (Name)
SELECT 'ANDREW JONES', 'JONE'
UNION
SELECT 'BILL A AND MARY B SMITH', 'SMIT'
UNION
SELECT 'TOM AND MARIA BAND', 'BAND'
UNION
SELECT 'STEVEN AND JESSICA E CODE', 'CODE'
UNION
SELECT 'VINCE AND MOLLY ALICE MATT', 'MATT'
NameCTR is the first 4 characters of the Last Name.
MARY B SMITH
MARIA BAND
JESSICA E CODE
MOLLY ALICE MATT
I have a hard to get the middle name.
Thanks
January 20, 2017 at 12:58 pm
Something like this?SELECT LEFT(N.Name, PATINDEX('% AND %', N.Name)-1) + RIGHT(N.Name, CHARINDEX(' ',REVERSE(N.Name))) AS FirstConsumer,
RIGHT(N.Name, LEN(N.Name) - (PATINDEX('% AND %', N.Name) + 4)) AS SecondConsumer
FROM #NAME N
WHERE N.Name LIKE '% AND %';
P.s. "I have a hard to get the middle name." is NOT the best way to say that in English 😉
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 20, 2017 at 1:08 pm
Thom A - Friday, January 20, 2017 12:58 PMP.s. "I have a hard to get the middle name." is NOT the best way to say that in English 😉
That's a statement from someone who really loves their coding.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
January 20, 2017 at 1:23 pm
Sorry I mean I have a hard time to get the middle name.
Thanks
January 20, 2017 at 1:26 pm
BTW I want the name in different columns.
First name - Mary
Middle Name - B
Last Name - SMITHThanks
January 20, 2017 at 1:47 pm
Maybe something like this:
SELECT LEFT( SecondName, CHARINDEX( ' ', SecondName) - 1) AS FirstName,
SUBSTRING( SecondName, CHARINDEX( ' ', SecondName) + 1, 800) AS MiddleName,
SUBSTRING( Name, LastNameStart, 800) AS LastName,
n.*
FROM #Name n
CROSS APPLY (SELECT CHARINDEX(' AND ', Name)+5 AS SecondStart, CHARINDEX(NameCTR, Name) AS LastNameStart)x
OUTER APPLY (SELECT SUBSTRING(Name, SecondStart, LastNameStart - SecondStart) SecondName
WHERE SecondStart > 5
AND LastNameStart > SecondStart)y;
I would strongly suggest that you correct this design to comply with the first normal form. That would make your queries a lot simpler and more efficient.
January 20, 2017 at 1:56 pm
Loner - Friday, January 20, 2017 1:26 PMBTW I want the name in different columns.First name - Mary
Middle Name - B
Last Name - SMITHThanks
Well, that changes the game.
My solution:WITH SC AS(
SELECT RIGHT(N.Name, LEN(N.Name) - (PATINDEX('% AND %', N.Name) + 4)) AS SecondConsumer
FROM #NAME N
WHERE N.Name LIKE '% AND %'),
SCS AS (
SELECT LEFT(SC.SecondConsumer, CHARINDEX(' ',SC.SecondConsumer) - 1) AS FirstName,
RIGHT(SC.SecondConsumer, CHARINDEX(' ',REVERSE(SC.SecondConsumer))-1) AS Surname,
*
FROM SC)
SELECT FirstName,
CASE WHEN LEN(SCS.Firstname) + LEN(SCS.Surname) + 1 < LEN(SCS.SecondConsumer)
THEN SUBSTRING(SCS.SecondConsumer, LEN(SCS.Firstname) +2 , LEN(SCS.SecondConsumer) - (LEN(SCS.Firstname) + LEN(SCS.Surname) + 1) - 1) END AS MiddleName,
SCS.Surname,
SecondConsumer
FROM SCS;
@luis, I would not be surprised if this is Vendor based.
EDIT: Slight amendment due to leading whitespace on middlename.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply