| Author |
Topic |
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-06 : 10:44:03
|
| in a client's table, there is a column for FirstName which, mistakenly, actually has the First Name, Middle Name and Suffix stuffed in there. There are seperate columns for MiddleName and Suffix which are all NULL right now. I would like to substring the firstname string out and put the everything after the first ' ' and before the second ' ' into MiddleName and anything after the second ' ' into Suffix.How would this look in a substring?thanks |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-06 : 11:35:08
|
Suffix is as you purport, there are no prefixes, and your script blows up on the second "AS":Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AS'.Sample data would look like: FIRSTNAME MIDDLENAME LASTNAME SUFFIXJohn Joseph Jr NULL DOE NULL |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-06 : 11:51:14
|
Removing the second "AS" provides:Spaces Space_Rows0 1186441 1173763 17NULL 72 7091 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-03-06 : 12:11:22
|
| my bad...copy and paste..oyeGood catchNow, do you see the counts?You look to have 118k+ rows that appear to be first name only117k+ that looks to be first and last (or last and first..idk)and 7k+ that are first middle last (or any combo)3 special guys that are?and 7 rows with nothingCan you get a sample of each? See if this runs together, if not break it up..and post what you get from thisSELECT TOP 10 LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) AS [Spaces], FirstNameFROM yourTableWHERE LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) = 0UNION ALLSELECT TOP 10 LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) AS [Spaces], FirstNameFROM yourTableWHERE LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) = 1UNION ALLSELECT TOP 10 LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) AS [Spaces], FirstNameFROM yourTableWHERE LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) = 2UNION ALLSELECT TOP 10 LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) AS [Spaces], FirstNameFROM yourTableWHERE LEN(FirstName) - LEN(REPLACE(FirstName,' ','')) = 3Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-06 : 12:19:22
|
| Spaces FirstName0 DONALD0 LYNN0 TINA0 FRANKLINSpaces FirstName1 BRYAN JOSEPH1 MELVIN DOUGLAS1 EDWARD LEE1 BOBBY CLYDE1 VICTOR SHERWINSpaces FirstName2 JAMES A III2 PARKS AVERY JR2 JAMES EDWARD JR2 KENNETH WESLEY JRSpaces FirstName3 A B JR MRS3 ROBERT G SR MRS3 JAMES M AKA WILLIAM3 ANDREW D J III3 ANDREW D J III |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-06 : 12:54:23
|
| Spaces = 0 Do nothing - correct-a-mundoSpaces = 1 first string is ALWAYS firstname and second can always go to MiddlenameSpaces = 2 First middle suffix - correct-a-mundoSpaces = 3 same rule as '2' but allow the last two segments into suffix.I have the following to parse the leading characters out of an address string. it 'seems' i could modify this somehow to get the job done: update mnione set housenuone =LEFT(addressline1,CASE WHEN CHARINDEX(' ',addressline1)> 0 THEN CHARINDEX(' ',addressline1) ELSE PATINDEX('%[A-Za-z]%',addressline1) END)WHERE addressline1 LIKE '[0-9]%' |
 |
|
|
|