I was trying to create a function from the script library and am having some problems.I have created and tested the pre-req' functions needed for this function.There were a couple of easy errors in the sample posted.1st line was alter procedure. No beggy.. changed to create.2nd error at bottom of codeUPDATE UsersSET Honorific = @H, FirstName = @F, MiddleName = @M, LastName = @L, Suffix = @SWHERE CURRENT OF cNames changed UPDATE Users to Update @Users to reflect the use of the temp table.Original code was here [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56499[/url]The Errors I am getting are... quote: Msg 207, Level 16, State 1, Line 0Invalid column name 'Honorific'.Msg 207, Level 16, State 1, Line 0Invalid column name 'FirstName'.Msg 207, Level 16, State 1, Line 0Invalid column name 'MiddleName'.Msg 207, Level 16, State 1, Line 0Invalid column name 'LastName'.Msg 207, Level 16, State 1, Line 0Invalid column name 'Suffix'.
Here is the Code after the 2 corrections.Create PROCEDURE NameParser(@NameString VARCHAR(100) = NULL, @Honorific VARCHAR(30) OUTPUT, @FirstName VARCHAR(30) OUTPUT, @MiddleName VARCHAR(30) OUTPUT, @LastName VARCHAR(30) OUTPUT, @Suffix VARCHAR(30) OUTPUT )ASBEGIN-- Dependent on: GetWordCount function (http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=51289)-- Dependent on: A Case insenitive collation--blindman, 11/04--FormatName parses a NameString into its component parts and returns it in a requested format.-- Jay - 12/2007 Completly re-written as a procedure with 4 OUTPUT parameters-- Remove anything in parens-- Support for 3-part names (Van Der, De La)-- Support for multi-part suffix ('MD, FACS', 'III, MD', etc)-- Various prefixs & suffixs (including some suffixs to clean up company names)-- If name matches an email address, returns email in the Last Name-- Strip numerics, &;#:/"*-()-- Strip stray (space), -, * from beginning/end of parsed name parts-- Procedure now dependent on GetWordCount() function-- Deals with C/O in a name-- General rules:-- - Begins (or ends) with c/o, just ignore it-- - Prefer the name before the c/o-- - if before is more than 3 parts and after is 2, or 3 parts - use after-- (May change depending on results, will attempt to make code easilly changable)-- Converted to a procedure that uses OUTPUT---- Added logic to "Last, First" names to include 3-part names and also act on the "," if there is no valid suffix.-- Moved possible Honorific, Suffix, 2-part last names and 3-part last names to a (local) table structure.-- If someone wants to, they can easilly move these values to real tables and maintain them in a seperate UI.--DECLARE @LastSuffix varchar(20)DECLARE @TempString varchar(100)DECLARE @TempString2 varchar(100)DECLARE @SaveNameString varchar(100)DECLARE @IgnorePeriod char(1)DECLARE @WordCount intDECLARE @Part1 varchar(20)DECLARE @Part2 varchar(20)DECLARE @ThreePart char(1)DECLARE @CO1IsCo char(1)DECLARE @CO2IsCo char(1)DECLARE @CO1words TINYINTDECLARE @CO2words TINYINTDECLARE @CO1Honorific varchar(30)DECLARE @CO1FirstName varchar(30)DECLARE @CO1MiddleName varchar(30)DECLARE @CO1LastName varchar(30)DECLARE @CO1Suffix varchar(30)DECLARE @CO2Honorific varchar(30)DECLARE @CO2FirstName varchar(30)DECLARE @CO2MiddleName varchar(30)DECLARE @CO2LastName varchar(30)DECLARE @CO2Suffix varchar(30)declare @OpenP tinyintdeclare @CloseP tinyintdeclare @Space1 tinyintdeclare @Space2 tinyintSET @Honorific = NULLSET @FirstName = NULLSET @MiddleName = NULLSET @LastName = NULLSET @Suffix = NULL if @NameString IS NULL RETURN-- Prefix tableDECLARE @Prefixs TABLE (Prefix varchar(30))insert into @Prefixs (Prefix) VALUES('Mr')insert into @Prefixs (Prefix) VALUES('Mrs')insert into @Prefixs (Prefix) VALUES('MrMrs') -- In data as 'Mr & Mrs'insert into @Prefixs (Prefix) VALUES('Ms')insert into @Prefixs (Prefix) VALUES('Miss')insert into @Prefixs (Prefix) VALUES('Dr')insert into @Prefixs (Prefix) VALUES('Drs')insert into @Prefixs (Prefix) VALUES('Doctor')insert into @Prefixs (Prefix) VALUES('Rev')insert into @Prefixs (Prefix) VALUES('Reverend')insert into @Prefixs (Prefix) VALUES('Sir')insert into @Prefixs (Prefix) VALUES('Hon')insert into @Prefixs (Prefix) VALUES('Honorable')insert into @Prefixs (Prefix) VALUES('CPL')insert into @Prefixs (Prefix) VALUES('Corporal')insert into @Prefixs (Prefix) VALUES('SGT')insert into @Prefixs (Prefix) VALUES('Sergeant')insert into @Prefixs (Prefix) VALUES('GEN')insert into @Prefixs (Prefix) VALUES('General')insert into @Prefixs (Prefix) VALUES('CMD')insert into @Prefixs (Prefix) VALUES('Commander')insert into @Prefixs (Prefix) VALUES('Cpt')insert into @Prefixs (Prefix) VALUES('Capt')insert into @Prefixs (Prefix) VALUES('Captain')insert into @Prefixs (Prefix) VALUES('Maj')insert into @Prefixs (Prefix) VALUES('Major')insert into @Prefixs (Prefix) VALUES('PVT')insert into @Prefixs (Prefix) VALUES('Private')insert into @Prefixs (Prefix) VALUES('LT')insert into @Prefixs (Prefix) VALUES('Lieutenant')insert into @Prefixs (Prefix) VALUES('Father')insert into @Prefixs (Prefix) VALUES('Sister')-- Suffix tableDECLARE @Suffixs TABLE (Suffix varchar(30),Type char(4))insert into @Suffixs (Suffix, Type) VALUES('Junior', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Senior', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Jr', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Sr', 'Name')insert into @Suffixs (Suffix, Type) VALUES('II', 'Name')insert into @Suffixs (Suffix, Type) VALUES('III', 'Name')insert into @Suffixs (Suffix, Type) VALUES('IV', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Esq', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CPE', 'Name')insert into @Suffixs (Suffix, Type) VALUES('GM', 'Name')insert into @Suffixs (Suffix, Type) VALUES('RN', 'Name')insert into @Suffixs (Suffix, Type) VALUES('BS', 'Name')insert into @Suffixs (Suffix, Type) VALUES('MD', 'Name')insert into @Suffixs (Suffix, Type) VALUES('M D', 'Name')insert into @Suffixs (Suffix, Type) VALUES('C H A.', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CHA', 'Name')insert into @Suffixs (Suffix, Type) VALUES('ST', 'Name')insert into @Suffixs (Suffix, Type) VALUES('DO', 'Name')insert into @Suffixs (Suffix, Type) VALUES('D O', 'Name')insert into @Suffixs (Suffix, Type) VALUES('PhD', 'Name')insert into @Suffixs (Suffix, Type) VALUES('ManagingDirector', 'Name')insert into @Suffixs (Suffix, Type) VALUES('MS', 'Name')insert into @Suffixs (Suffix, Type) VALUES('RD', 'Name')insert into @Suffixs (Suffix, Type) VALUES('LD', 'Name')insert into @Suffixs (Suffix, Type) VALUES('DO', 'Name')insert into @Suffixs (Suffix, Type) VALUES('PC', 'Name')insert into @Suffixs (Suffix, Type) VALUES('DPM', 'Name')insert into @Suffixs (Suffix, Type) VALUES('FACS', 'Name')insert into @Suffixs (Suffix, Type) VALUES('EdM', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Chairman', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CPM', 'Name')insert into @Suffixs (Suffix, Type) VALUES('LPN', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Esthetician', 'Name')insert into @Suffixs (Suffix, Type) VALUES('ARNP', 'Name')insert into @Suffixs (Suffix, Type) VALUES('PE', 'Name')insert into @Suffixs (Suffix, Type) VALUES('LSCW', 'Name')insert into @Suffixs (Suffix, Type) VALUES('DC', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CMA', 'Name')insert into @Suffixs (Suffix, Type) VALUES('JD', 'Name')insert into @Suffixs (Suffix, Type) VALUES('DMH', 'Name')insert into @Suffixs (Suffix, Type) VALUES('VP', 'Name')insert into @Suffixs (Suffix, Type) VALUES('FPMSI', 'Name')insert into @Suffixs (Suffix, Type) VALUES('PMB', 'Name')insert into @Suffixs (Suffix, Type) VALUES('PTY', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CMT', 'Name')insert into @Suffixs (Suffix, Type) VALUES('CFP', 'Name')insert into @Suffixs (Suffix, Type) VALUES('MBE', 'Name')insert into @Suffixs (Suffix, Type) VALUES('SKC', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Plc', 'Name')insert into @Suffixs (Suffix, Type) VALUES('Ltd', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Co', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Inc', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Ent', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Tech', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Technology', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Technologies', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Systems', 'Co')insert into @Suffixs (Suffix, Type) VALUES('USA', 'Co')insert into @Suffixs (Suffix, Type) VALUES('LLC', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Environments', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Clinic', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Society', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Architects', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Communications', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Hospital', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Hosp', 'Co')insert into @Suffixs (Suffix, Type) VALUES('University', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Univ', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Associates', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Conference', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Assistant', 'Co')insert into @Suffixs (Suffix, Type) VALUES('Intl', 'Co')-- TwoPartNames tableDECLARE @Twoparts TABLE (TwoPart varchar(30)) insert into @TwoParts (TwoPart) VALUES('VAN')insert into @TwoParts (TwoPart) VALUES('VON')insert into @TwoParts (TwoPart) VALUES('MC')insert into @TwoParts (TwoPart) VALUES('Mac')insert into @TwoParts (TwoPart) VALUES('DE')insert into @TwoParts (TwoPart) VALUES('St')insert into @TwoParts (TwoPart) VALUES('St.')insert into @TwoParts (TwoPart) VALUES('Le')insert into @TwoParts (TwoPart) VALUES('Di')insert into @TwoParts (TwoPart) VALUES('La')insert into @TwoParts (TwoPart) VALUES('Al')insert into @TwoParts (TwoPart) VALUES('Da')insert into @TwoParts (TwoPart) VALUES('El')insert into @TwoParts (TwoPart) VALUES('Dal')insert into @TwoParts (TwoPart) VALUES('Del')insert into @TwoParts (TwoPart) VALUES('Sta')insert into @TwoParts (TwoPart) VALUES('Ste')-- ThreePartNames tableDECLARE @Threeparts TABLE (ThreePart varchar(30)) insert into @ThreeParts (ThreePart) VALUES('de sta')insert into @ThreeParts (ThreePart) VALUES('de ste')insert into @ThreeParts (ThreePart) VALUES('de la')insert into @ThreeParts (ThreePart) VALUES('del la')insert into @ThreeParts (ThreePart) VALUES('van der')insert into @ThreeParts (ThreePart) VALUES('van de')--Strip any leading non-alphanumericwhile patindex('[^a-z0-9]%', @NameString) = 1 SET @NameString = SUBSTRING(@NameString, 2, LEN(@NameString))--print 'NameString_01=[' + @NameString + ']'-- If this is an email address, return it immediatly (Jay)if @NameString LIKE '%@%.%'beginwhile charindex(' ', @NameString) > 0 set @NameString = replace(@NameString, ' ', '')SELECT@Honorific = '', @FirstName = '', @MiddleName = '', @LastName = @NameString, @Suffix = ''RETURNend-- Whack anything in ()'s -- The use is very variable and though it could be parsed, I'm chosing not to.set @OpenP = CHARINDEX('(', @NameString)set @CloseP = CHARINDEX(')', @NameString)if @OpenP < @CloseP and @OpenP > 0begin--print 'Open: ' + CAST(@OpenP AS VARCHAR) + ' - ' + CAST(@CloseP AS VARCHAR)set @NameString = substring(@NameString, 1, @OpenP-1) + ' ' + substring(@NameString, @CloseP+1, 999)while charindex(' ', @NameString) > 0 set @NameString = replace(@NameString, ' ', ' ')--print '[' + @NameString + ']'end-- Initial c/o parsingif ( @NameString LIKE '%C\O%' ) set @NameString = replace(@NameString, 'c\o', 'C/O')if ( @NameString LIKE '%C.O.%' ) set @NameString = replace(@NameString, 'c.o.', 'C/O')if ( @NameString LIKE 'C/O%' )set @NameString = SUBSTRING(@NameString, 4, LEN(@NameString))if ( @NameString LIKE '%C/O%' )beginset @CO1words = dbo.GetWordCount(SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1), NULL)set @CO2words = dbo.GetWordCount(SUBSTRING(@NameString, PATINDEX('%C/O%', @NameString)+3, LEN(@NameString)), NULL)--set @CO1Suffix = dbo.FormatName(SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1), 's') -- XXX--set @CO2Suffix = dbo.FormatName(SUBSTRING(@NameString, PATINDEX('%C/O%', @NameString)+3, LEN(@NameString)), 's') -- XXX Old function, make recursiveset @TempString = SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1)EXECdbo.NameParser @NameString = @TempString, @Honorific = @CO1Honorific OUTPUT, @FirstName = @CO1FirstName OUTPUT, @MiddleName = @CO1MiddleName OUTPUT, @LastName = @CO1LastName OUTPUT, @Suffix = @CO1Suffix OUTPUTset @TempString = SUBSTRING(@NameString, PATINDEX('%C/O%', @NameString)+3, LEN(@NameString))EXECdbo.NameParser @NameString = @TempString, @Honorific = @CO2Honorific OUTPUT, @FirstName = @CO2FirstName OUTPUT, @MiddleName = @CO2MiddleName OUTPUT, @LastName = @CO2LastName OUTPUT, @Suffix = @CO2Suffix OUTPUT-- Decide to use name 1, or name 2. Prefer a persons name.if @CO1Suffix in (SELECT SuffixFROM @SuffixsWHERE Type = 'Co')or SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1) LIKE 'The %' -- Sometimes "The Smith's", but the rule will still applyset @CO1IsCo = 'Y'elseset @CO1IsCo = 'N'if @CO2Suffix in (SELECT SuffixFROM @SuffixsWHERE Type = 'Co')or SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1) LIKE 'The %' -- Sometimes "The Smith's", but the rule will still applyset @CO2IsCo = 'Y'elseset @CO2IsCo = 'N'if ( @CO1IsCo = 'Y' AND @CO2IsCo = 'N' )beginset @NameString = SUBSTRING(@NameString, PATINDEX('%C/O%', @NameString)+3, LEN(@NameString))endelsebeginif ( @CO1IsCo = 'N' AND @CO2IsCo = 'Y' )beginset @NameString = SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1)endelsebegin-- Are they both company names?if ( @CO1IsCo = 'Y' AND @CO2IsCo = 'Y' )begin-- Use 1stset @NameString = SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1)endelsebegin-- A 2, or 3 word name is prefered to a 1, 4, or more part nameif ( @CO1words = 2 or @CO1words = 3 ) and ( @CO2words = 1 or @CO2words >= 4 )beginset @NameString = SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1)endelsebeginif ( @CO2words = 2 or @CO2words = 3 ) and ( @CO1words = 1 or @CO1words >= 4 )beginset @NameString = SUBSTRING(@NameString, PATINDEX('%C/O%', @NameString)+3, LEN(@NameString))endelsebegin-- Give up and use 1stset @NameString = SUBSTRING(@NameString, 1, PATINDEX('%C/O%', @NameString)-1)end end -- 2,3 word over 1, 4+end -- IsCo Y/Yend -- IsCo N/Yend -- else IsCo Y/Nend--Prepare the string--Make sure each period and comma is followed by a space character.set @NameString = rtrim(ltrim(replace(@NameString, '.', '. ')))set @NameString = rtrim(ltrim(replace(@NameString, ',', ', ')))-- Catch Suffix's --Eliminate periodswhile charindex('.', @NameString) > 0 set @NameString = replace(@NameString, '.', ' ')--Eliminate numericswhile charindex('0', @NameString) > 0 set @NameString = replace(@NameString, '0', ' ')while charindex('1', @NameString) > 0 set @NameString = replace(@NameString, '1', ' ')while charindex('2', @NameString) > 0 set @NameString = replace(@NameString, '2', ' ')while charindex('3', @NameString) > 0 set @NameString = replace(@NameString, '3', ' ')while charindex('4', @NameString) > 0 set @NameString = replace(@NameString, '4', ' ')while charindex('5', @NameString) > 0 set @NameString = replace(@NameString, '5', ' ')while charindex('6', @NameString) > 0 set @NameString = replace(@NameString, '6', ' ')while charindex('7', @NameString) > 0 set @NameString = replace(@NameString, '7', ' ')while charindex('8', @NameString) > 0 set @NameString = replace(@NameString, '8', ' ')while charindex('9', @NameString) > 0 set @NameString = replace(@NameString, '9', ' ')--Eliminate &while charindex('&', @NameString) > 0 set @NameString = replace(@NameString, '&', ' ')--Eliminate ;while charindex(';', @NameString) > 0 set @NameString = replace(@NameString, ';', ' ')--Eliminate :while charindex(':', @NameString) > 0 set @NameString = replace(@NameString, ':', ' ')--Eliminate #while charindex('#', @NameString) > 0 set @NameString = replace(@NameString, '#', ' ')--Eliminate /while charindex('/', @NameString) > 0 set @NameString = replace(@NameString, '/', ' ')--Eliminate (while charindex('(', @NameString) > 0 set @NameString = replace(@NameString, '(', ' ')--Eliminate )while charindex(')', @NameString) > 0 set @NameString = replace(@NameString, ')', ' ')--Eliminate "while charindex('"', @NameString) > 0 set @NameString = replace(@NameString, '"', ' ')--Eliminate *while charindex('*', @NameString) > 0 set @NameString = replace(@NameString, '*', ' ')--Eliminate _ -> (space)while charindex('_', @NameString) > 0 set @NameString = replace(@NameString, '_', ' ')--Eliminate double-spaces.while charindex(' ', @NameString) > 0 set @NameString = replace(@NameString, ' ', ' ')--Strip remaining spacesSET @NameString = LTRIM(RTRIM(@NameString))--print 'NameString_03=[' + @NameString + ']'-- Change suffix's-- 'M.D.' becomes 'M D' to 'MD' so the suffix code will catch itif substring(@NameString, LEN(@NameString)-2, 3) = 'M D' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-3) + 'MD'if substring(@NameString, LEN(@NameString)-3, 4) = 'Ph D' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-4) + 'PhD'if substring(@NameString, LEN(@NameString)-2, 3) = 'D O' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-3) + 'DO'if substring(@NameString, LEN(@NameString)-4, 5) = 'D P M' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-5) + 'DPM'if substring(@NameString, LEN(@NameString)-6, 7) = 'F A C S' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-7) + 'FACS'if substring(@NameString, LEN(@NameString)-16, 17) = 'Managing Director' set @NameString = SUBSTRING(@NameString, 1, LEN(@NameString)-17) + 'ManagingDirector'-- If the lastname is listed first, strip it off.-- Only do this if the comma isn't in position 2. Trying to avoid single character last names and people using a , when a . is correct (or Europeian)if patindex('_,%', @NameString) = 0 and patindex('%,%', @NameString) > 0beginset @Space1 = charindex(' ', @NameString)-- Catch 3-part last names in "Last, First" format -> 'De la Rosa, Marie'if patindex('% % %', @NameString) > 0beginset @Space2 = charindex(' ', @NameString, @Space1 + 1)set @TempString = substring(@NameString, 1, @Space2-1)if @TempString IN ( SELECT ThreePart FROM @ThreeParts )set @Lastname = substring(@NameString, 1, charindex(',', @NameString))endif @Lastname IS NULLbegin-- Catch 2-part names in "Last, First" format -> Von Hussan, Mikeset @TempString = rtrim(left(@NameString, charindex(' ', @NameString))) -- Gets the 1st word in the stringif @TempString in ( SELECT TwoPart FROM @TwoParts ) set @TempString = rtrim(left(@NameString, charindex(' ', @NameString, len(@TempString)+2)))if right(@TempString, 1) = ',' set @LastName = left(@TempString, len(@TempString)-1)endif len(@LastName) > 0 beginset @NameString = ltrim(right(@NameString, len(@NameString) - len(@TempString)))endelsebegin-- Could be some other "Last, First" if there isn't a suffix on the nameset @TempString2 = reverse(@NameString)set @TempString = rtrim(ltrim(reverse(substring(@TempString2, 1, charindex(',', @TempString2)-1))))if @TempString NOT in (SELECT Suffix FROM @Suffixs)beginset @LastName = substring(@NameString, 1, charindex(',', @NameString)-1)set @NameString = ltrim(rtrim(substring(@NameString, charindex(',', @NameString)+1, 999)))endendend--Get rid of any remaining commaswhile charindex(',', @NameString) > 0 set @NameString = replace(@NameString, ',', '')--print '[' + @NameString + ']'--Get Honorific and strip it out of the @NameStringif dbo.GetWordCount(@NameString, NULL) >= 2begin-- Manual adjustmentsif @NameString like 'mrss[., ]' set @NameString = 'Mrs ' + SUBSTRING(@NameString, 6, LEN(@NameString))if @NameString like 'Mr Mrs %' set @NameString = 'MrMrs ' + SUBSTRING(@NameString, 8, LEN(@NameString))set @TempString = rtrim(left(@NameString, charindex(' ', @NameString + ' ')))if @TempString in (SELECT Prefix FROM @Prefixs)set @Honorific = @TempString-- Manual adjustmentsif @Honorific = 'MrMrs' set @Honorific = 'Mr & Mrs'if len(@Honorific) > 0beginset @NameString = ltrim(right(@NameString, len(@NameString) - len(@TempString)))endend--print 'NameString_05=[' + @NameString + '], H=[' + ISNULL(@Honorific, '') + '], F=[' + ISNULL(@FirstName, '') + '], M=[' + ISNULL(@MiddleName, '') + '], L=[' + ISNULL(@LastName, 'NULL') + '], S=[' + ISNULL(@Suffix, '') + ']'-- Deal with multiple suffixs (Jay)--Get Suffix and strip it out of the stringif dbo.GetWordCount(@NameString, NULL) >= 2 and NOT (dbo.GetWordCount(@NameString, NULL) = 3 and @NameString LIKE '% [A-Z] %')beginset @Suffix = ''set @LastSuffix = ''while 1 = 1 begin-- Manual suffix adjustmentsif @NameString LIKE '%[ .,]M D' SET @NameString = substring(@NameString, 1, LEN(@NameString)-3) + 'MD'if @NameString LIKE '%[ ,.]C H A[,. ]%' OR @NameString LIKE '%[ ,.]C H A'begin-- Cut the existing out and replace it with 'CHA'SET @NameString = SUBSTRING(@NameString, 1, PATINDEX('%[ ,.]C H A%', @NameString))+ 'CHA'+ SUBSTRING(@NameString, PATINDEX('%[ ,.]C H A%', @NameString) + 6, LEN(@NameString))endset @TempString = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' ')))if @TempString in (SELECT Suffix FROM @Suffixs)beginif @Suffix <> @TempString beginif len(@Suffix) > 0set @Suffix = @TempString + ', ' + @Suffixelseset @Suffix = @TempStringset @LastSuffix = @Suffixif len(@Suffix) > 0 set @NameString = rtrim(left(@NameString, len(@NameString) - len(@TempString)))endendelsebeginbreakendendend--print 'NameString_06=[' + @NameString + '], H=[' + ISNULL(@Honorific, '') + '], F=[' + ISNULL(@FirstName, '') + '], M=[' + ISNULL(@MiddleName, '') + '], L=[' + ISNULL(@LastName, 'NULL') + '], S=[' + ISNULL(@Suffix, '') + ']'-- Count the number or words in the @NameStringSET @WordCount = dbo.GetWordCount(@NameString, NULL)--print 'Word count=' + cast(@WordCount AS VARCHAR)if @LastName is null OR LEN(@LastName) = 0begin--Get LastName and strip it out of the stringset @LastName = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' ')))set @NameString = rtrim(left(@NameString, len(@NameString) - len(@LastName)))-- Check for a three part last name (before the two part)set @ThreePart = 'N'if @WordCount >= 4 -- 4 because I'm assuming a first namebegin-- Extract the last & 2nd to last wordsset @SaveNameString = @NameStringset @Part2 = ltrim(rtrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' '))))set @NameString = substring(@NameString, 1, LEN(@NameString) - LEN(@Part2) - 1 )set @Part1 = ltrim(rtrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' '))))set @NameString = substring(@NameString, 1, LEN(@NameString) - LEN(@Part1) - 1 )set @TempString = @Part1 + ' ' + @Part2if @TempString in ( SELECT ThreePart FROM @ThreeParts )beginset @LastName = @TempString + ' ' + @LastNameset @ThreePart = 'Y'endelsebegin-- No match, reset the @NameStringset @NameString = @SaveNameStringendendif @WordCount > 2 and @ThreePart = 'N'begin--Check to see if the last name has two partsset @TempString = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' ')))if @TempString in ( SELECT TwoPart FROM @TwoParts )beginset @LastName = @TempString + ' ' + @LastNameset @NameString = rtrim(left(@NameString, len(@NameString) - len(@TempString)))endendend--print 'NameString_07=[' + @NameString + '], H=[' + ISNULL(@Honorific, '') + '], F=[' + ISNULL(@FirstName, '') + '], M=[' + ISNULL(@MiddleName, '') + '], L=[' + ISNULL(@LastName, '') + '], S=[' + ISNULL(@Suffix, '') + ']'-- Strip any leading stray -' characters from the LastNameif len(@LastName) > 0while ( @LastName LIKE '[*--'']%' ESCAPE '-' )SET @LastName = RTRIM(SUBSTRING(@LastName, 2, LEN(@LastName)-1)) -- Strip any trailing stray -' characters from the LastNameif len(@LastName) > 0while ( @LastName LIKE '%[*--'']' ESCAPE '-' )SET @LastName = RTRIM(SUBSTRING(@LastName, 1, LEN(@LastName)-1)) --Get FirstName and strip it out of the stringset @FirstName = ltrim(rtrim(left(@NameString, charindex(' ', @NameString + ' '))))-- Strip any leading stray -' characters from the FirstNameif len(@FirstName) > 0while ( @FirstName LIKE '[*--'']%' ESCAPE '-' )SET @FirstName = RTRIM(SUBSTRING(@FirstName, 2, LEN(@FirstName))) -- Strip any trailing stray -' characters from the FirstNameif len(@FirstName) > 0while ( @FirstName LIKE '%[*--'']' ESCAPE '-' )SET @FirstName = RTRIM(SUBSTRING(@FirstName, 1, LEN(@FirstName)-1))set @NameString = ltrim(right(@NameString, len(@NameString) - len(@FirstName)))--print 'NameString_08=[' + @NameString + '], H=[' + ISNULL(@Honorific, '') + '], F=[' + ISNULL(@FirstName, '') + '], M=[' + ISNULL(@MiddleName, '') + '], L=[' + ISNULL(@LastName, '') + '], S=[' + ISNULL(@Suffix, '') + ']'--Anything remaining is MiddleNameset @MiddleName = LTRIM(RTRIM(@NameString))-- Strip any leading stray -' characters from the MiddleNameif len(@MiddleName) > 0while ( @MiddleName LIKE '[ *--'']%' ESCAPE '-' )SET @MiddleName = RTRIM(SUBSTRING(@MiddleName, 2, LEN(@MiddleName)-1)) -- Strip any trailing stray -' characters from the MiddleNameif len(@MiddleName) > 0while ( @MiddleName LIKE '%[ *--'']' ESCAPE '-' )SET @MiddleName = RTRIM(SUBSTRING(@MiddleName, 1, LEN(@MiddleName)-1))-- Return '', not NULLif @Honorific IS NULL set @Honorific = ''if @FirstName IS NULL set @FirstName = ''if @MiddleName IS NULL set @MiddleName = ''if @LastName IS NULL set @LastName = ''if @Suffix IS NULL set @Suffix = ''/*--Create the output stringset @TempString = ''while len(@NameFormat) > 0beginif @IgnorePeriod = 'F' or left(@NameFormat, 1) <> '.'beginset @IgnorePeriod = 'F'set @TempString = @TempString +case ascii(left(@NameFormat, 1))when '72' then case @Honorificwhen 'Dr' then 'Doctor'when 'Rev' then 'Reverend'when 'Hon' then 'Honorable'when 'Maj' then 'Major'when 'Pvt' then 'Private'when 'Lt' then 'Lieutenant'when 'Capt' then 'Captain'when 'Cpt' then 'Captain'when 'Cmd' then 'Commander'when 'Gen' then 'General'when 'Sgt' then 'Sergeant'when 'Cpl' then 'Corporal'else isnull(@Honorific, '')endwhen '70' then isnull(@FirstName, '')when '77' then isnull(@MiddleName, '')when '76' then isnull(@LastName, '')when '83' then case @Suffixwhen 'Jr' then 'Junior'when 'Sr' then 'Senior'when 'Esq' then 'Esquire'else isnull(@Suffix, '')endwhen '104' then case @Honorificwhen 'Doctor' then 'Dr'when 'Reverend' then 'Rev'when 'Honorable' then 'Hon'when 'Major' then 'Maj'when 'Private' then 'Pvt'when 'Lieutenant' then 'Lt'when 'Captain' then 'Capt'when 'Cpt' then 'Capt'when 'Commander' then 'Cmd'when 'General' then 'Gen'when 'Sergeant' then 'Sgt'when 'Corporal' then 'Cpl'else isnull(@Honorific, '')endwhen '102' then isnull(left(@FirstName, 1), '')when '109' then isnull(left(@MiddleName, 1), '')when '108' then isnull(left(@LastName, 1), '')when '115' then case @Suffixwhen 'Junior' then 'Jr'when 'Senior' then 'Sr'when 'Esquire' then 'Esq'else isnull(@Suffix, '')endwhen '46' then case right(@TempString, 1)when ' ' then ''else '.'endwhen '44' then case right(@TempString, 1)when ' ' then ''else ','endwhen '32' then case right(@TempString, 1)when ' ' then ''else ' 'endelse ''endif ((ascii(left(@NameFormat, 1)) = 72 and @Honorific in ('FATHER', 'SISTER'))or (ascii(left(@NameFormat, 1)) = 115 and @Suffix in ('II', 'III')))set @IgnorePeriod = 'T'endset @NameFormat = right(@NameFormat, len(@NameFormat) - 1)end*/--print 'NameString_09=[' + @NameString + '], H=[' + ISNULL(@Honorific, '') + '], F=[' + ISNULL(@FirstName, '') + '], M=[' + ISNULL(@MiddleName, '') + '], L=[' + ISNULL(@LastName, '') + '], S=[' + ISNULL(@Suffix, '') + ']'Return --@TempStringendgoSET NOCOUNT ONDECLARE @idx INTDECLARE @UserNum INTDECLARE @NameIn varchar(100)DECLARE @H varchar(30)DECLARE @F varchar(30)DECLARE @M varchar(30)DECLARE @L varchar(30)DECLARE @S varchar(30)DECLARE @SmallRun char(1)SET @SmallRun = 'Y'SET @SmallRun = 'N'IF @SmallRun = 'Y'BEGINDECLARE @Users TABLE (Name varchar(100))SET @idx = 0INSERT INTO @Users (Name) Values('First last c/o Company & Co.')INSERT INTO @Users (Name) Values('Company inc. c/o First M Last')INSERT INTO @Users (Name) Values('Mr. First Last, Jr')INSERT INTO @Users (Name) Values('A F First Last, PhD.')INSERT INTO @Users (Name) Values('First M Last, Ph.D.')INSERT INTO @Users (Name) Values('First Middle Last, PhD')INSERT INTO @Users (Name) Values('Sir. First Last, III, Esq, PhD')INSERT INTO @Users (Name) Values('Mr & Mrs First last, III, Esq, PhD')INSERT INTO @Users (Name) Values('Reverend Gregory Robert Von Finzer Junior')INSERT INTO @Users (Name) Values('First de la ThreePartLast')INSERT INTO @Users (Name) Values('De la Last, First')INSERT INTO @Users (Name) Values('Von Last, First')INSERT INTO @Users (Name) Values('First von Last')INSERT INTO @Users (Name) Values('Last, First First')INSERT INTO @Users (Name) Values('Last last last, First')INSERT INTO @Users (Name) Values('Last, First M')INSERT INTO @Users (Name) Values('Last last, First')INSERT INTO @Users (Name) Values('First Last, MD')DECLARE cNames CURSOR FOR SELECT NameFROM @UsersWHERE Name IS NOT NULLAND LEN(Name) > 1--AND Name LIKE '%(%)%'OPEN cNamesFETCH cNames INTO @NameInWHILE @@FETCH_STATUS = 0BEGINSET @idx = @idx + 1IF LEN(LTRIM(RTRIM(ISNULL(@NameIn, '')))) = 0 CONTINUESELECT @H = '', @F = '', @L = '', @S = ''EXEC NameParser @NameIn, @Honorific = @H OUTPUT, @FirstName = @F OUTPUT, @MiddleName = @M OUTPUT, @LastName = @L OUTPUT, @Suffix = @S OUTPUTPRINT CAST(@idx AS CHAR(6)) + ' - In=[' + @NameIn + '], H=[' + ISNULL(@H, 'NULL') + '], F=[' + ISNULL(@F, 'NULL') + '], M=[' + ISNULL(@M, 'NULL') + '], L=[' + ISNULL(@L, 'NULL') + '], S=[' + ISNULL(@S, 'NULL') + ']'--SELECT @NameIn Name, ISNULL(@H, '') Honorific, ISNULL(@F, '') First, ISNULL(@M, '') Middle, ISNULL(@L, '') Last, ISNULL(@S, '') SuffixFETCH cNames INTO @NameInENDCLOSE cNamesDEALLOCATE cNamesENDELSEBEGINSET @idx = 0DECLARE cNames CURSOR FOR SELECT UserNum, NameFROM UsersWHERE Name IS NOT NULLAND LEN(Name) > 1--AND Name LIKE '%(%)%'OPEN cNamesFETCH cNames INTO @UserNum, @NameInWHILE @@FETCH_STATUS = 0BEGINSET @idx = @idx + 1IF LEN(LTRIM(RTRIM(ISNULL(@NameIn, '')))) = 0 CONTINUESELECT @H = '', @F = '', @L = '', @S = ''EXEC NameParser @NameIn, @Honorific = @H OUTPUT, @FirstName = @F OUTPUT, @MiddleName = @M OUTPUT, @LastName = @L OUTPUT, @Suffix = @S OUTPUTPRINT CAST(@idx AS CHAR(6)) + ' - In=[' + @NameIn + '], H=[' + ISNULL(@H, 'NULL') + '], F=[' + ISNULL(@F, 'NULL') + '], M=[' + ISNULL(@M, 'NULL') + '], L=[' + ISNULL(@L, 'NULL') + '], S=[' + ISNULL(@S, 'NULL') + ']'--SELECT @NameIn Name, ISNULL(@H, '') Honorific, ISNULL(@F, '') First, ISNULL(@M, '') Middle, ISNULL(@L, '') Last, ISNULL(@S, '') SuffixUPDATE @UsersSET Honorific = @H, FirstName = @F, MiddleName = @M, LastName = @L, Suffix = @SWHERE CURRENT OF cNamesFETCH cNames INTO @UserNum, @NameInENDCLOSE cNamesDEALLOCATE cNamesENDreturnThank You,John Fuhrmanhttp://www.titangs.com |