Author |
Topic |
aslu63
Starting Member
7 Posts |
Posted - 2012-03-15 : 16:09:35
|
Hello All,I have 7 different scenarios in my Full_Name field.Example:--1 SMITH;ELDON --2 SMITH,ELDON --3 SMITH,ELDON WILLIAM--4 E W SMITH--5 ELDON W--6 ELDON W SMITH--7 ELDON SMITHI need split to Last, First and Middle nameHow I can do it?The most concern when I have First Middle Last Name, How I can say SQL after first space middle name or Last Name?Thank you in advanced |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-15 : 16:13:08
|
so what should be output for each of above cases? can you show corresponding parts for each?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
aslu63
Starting Member
7 Posts |
Posted - 2012-03-15 : 16:20:43
|
I have 3 columnsand output should befor last_name : SMITH first_Name: ELDON Middle_N : last_name : SMITH first_Name: ELDON Middle_N : last_name : SMITH first_Name: ELDON Middle_N : WILLIAM last_name : SMITH first_Name: E Middle_N : W |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-15 : 16:33:20
|
so its not consistent. then i feel you need to break it up to groups and then apply logic for each one to get correct output------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-15 : 16:38:47
|
say for one group you can do like belowSELECT CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),1)END AS LastName,CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),1) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2)END AS FirstName,NULL AS MiddleNameFROM tableWHERE LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'))-LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'))=1and next one likeSELECT CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),3)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),3) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),1)END AS LastName,CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),3)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),1) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),3)END AS FirstName,PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2) AS MiddleNameFROM tableWHERE LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'))-LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'))=2etc... ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
aslu63
Starting Member
7 Posts |
Posted - 2012-03-16 : 09:13:48
|
I get error msg: The replace function requires 3 argument(s) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-16 : 11:18:53
|
there was a typo. use thisSELECT CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),2)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),2) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),1)END AS LastName,CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.')),' ','.'),2)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),1) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),2)END AS FirstName,NULL AS MiddleNameFROM tableWHERE LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'))-LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'))=1and next one likeSELECT CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),3)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),3) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),1)END AS LastName,CASE WHEN LEN(PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),3)) >1 THEN PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),1) ELSE PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),3)END AS FirstName,PARSENAME(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'),2) AS MiddleNameFROM tableWHERE LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'))-LEN(REPLACE(REPLACE(REPLACE(Full_Name,';','.'),',','.'),' ','.'))=2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|