I will add your examples in a memory table so we can test them as below:- (you dont need this)
Declare @NameTable table (fullname varchar(250))
insert into @NameTable values
('LASTNAME,FIRSTNAME MIDDLENAME'),
('LASTNAME,FIRSTNAME,MIDDLENAME'),
('LAST NAME,FIRSTNAME MIDDLENAME'),
('LAST NAME,FIRSTNAME (MIDDLENAME)'),
('LASTNAME,FIRSTNAME (NICKNAME) MIDDLENAME'),
('LASTNAME,FIRSTNAME') -- no middle name
Lets try to break it to some smaller steps, first get the brackets out, and then the last name and continue till we get all the parts.
;with FindBrackts as (
/* get rid of brackets */
select *,CHARINDEX('(',fullname) bStart,CHARINDEX(')',fullname) bEnd from @NameTable
),RemoveBrackts as (
select case when bStart>0 then substring(fullname,1,bStart-1)+substring(fullname,bEnd+2,len(fullname)-bEnd+1)
else fullname end fullname from FindBrackts)
,LastNameAndTheRest as (
select substring(fullname,1,CHARINDEX(',',fullname)-1) [LASTNAME]
,substring(fullname,CHARINDEX(',',fullname)+1,len(fullname)-CHARINDEX(',',fullname)) [TheRest] from RemoveBrackts
),LastFirstMiddle as (
select [LASTNAME],[TheRest],CHARINDEX(',',replace([TheRest],' ',',')) [mStart] from LastNameAndTheRest
)
select [LASTNAME]
,case when mStart=0 then [TheRest] else substring([TheRest],1,[mStart]-1) end FIRSTNAME
,case when mStart=0 then null else substring([TheRest],[mStart]+1,len([TheRest])-[mStart]) end MIDDLENAME
from LastFirstMiddle
The result will be something like the below:-
LASTNAME FIRSTNAME MIDDLENAME
======== ========= ==========
LASTNAME FIRSTNAME MIDDLENAME
LASTNAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME NULL
LASTNAME FIRSTNAME MIDDLENAME
If this helps and you find out there are more conditions please add them and may update the query to take them in consideration.
here is the same on SQL Fiddle http://sqlfiddle.com/#!18/2dd42/2/0
Or if you dont want any CTE, its a bit long but you can try the below:-
select
SUBSTRING(fullname, 0, CHARINDEX(',', fullname)) AS LastName
,SUBSTRING(replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','),1,CHARINDEX(',',replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','))-1) [FirstName]
,SUBSTRING(fullname,2+len(fullname)-CHARINDEX(',',reverse(replace(fullname,' ',','))),len(fullname)) MiddleName
from @NameTable
Results will be as a below, (a bit different than the previous solution)
LastName FirstName MiddleName
======== ========= ==========
LASTNAME FIRSTNAME MIDDLENAME
LASTNAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME (MIDDLENAME)
LASTNAME FIRSTNAME MIDDLENAME
if there is no Middlename , the CTE will handle it, but the select will need to be updated to the below:-
select
SUBSTRING(fullname, 0, CHARINDEX(',', fullname)) AS LastName
,case when CHARINDEX(',',replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','))>0 then
SUBSTRING(replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','),1,CHARINDEX(',',replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','))-1)
else replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',',') end [FirstName]
,case when CHARINDEX(',',replace(SUBSTRING(fullname,CHARINDEX(',', fullname)+1,len(fullname)-CHARINDEX(',', fullname)),' ',','))>0 then
SUBSTRING(fullname,2+len(fullname)-CHARINDEX(',',reverse(replace(fullname,' ',','))),len(fullname))
else NULL end MiddleName
from @NameTable
This is a bit long and may be it could be written shorter. anyhow the results would be as below:-
LastName FirstName MiddleName
========= ========= ==========
LASTNAME FIRSTNAME MIDDLENAME
LASTNAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME MIDDLENAME
LAST NAME FIRSTNAME (MIDDLENAME)
LASTNAME FIRSTNAME MIDDLENAME
LASTNAME FIRSTNAME NULL