Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2006-07-25 : 15:38:23
|
| I have a field called AdmittingPhysician. It contains data that looks like 'LastName III, FirstName A'I would like to strip down this field so that all I get back isLastName, FirstName. Is there anyway to do this? I for the life of me can't figure this one out. Thanks for taking a look.HC |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-07-25 : 16:07:56
|
| Need more examples or explain ur data clearly.i assume 'LastName III' & 'FirstName A' as 2 different data elements.Do u always have 2 words and u want to remove the second word.Do u have some words and need to remove the right most word ?Do u always have LastName and Something Else and keep the Last Name only ?What if the Data is like 'De Silva P' ?Srinika |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2006-07-25 : 16:18:10
|
| All this data is in one field'LastName III,FirstName A''LastName III,FirstName''LastName,FirstName A''LastName,FirstName'Data is in one of these four formats, and I want all the data to return like this'LastName,FirstName'If the record was 'De Silva III,Paul A', I would want 'De Silva,Paul'Thanks alot |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-25 : 16:34:26
|
This code might work for most cases-- Prepare test datadeclare @s table (s varchar(200))insert @sselect 'LastName III, FirstName A' union allselect 'LastName, FirstName'-- Do the workselect z.s Original, case when z.p1 > z.p2 then ltrim(rtrim(left(z.s, z.p2 - 1))) else rtrim(ltrim(left(z.s, z.p1))) end LastName, case when z.p1 > z.p2 then ltrim(rtrim(substring(z.s, z.p2 + 1, z.p4 - z.p2))) else ltrim(rtrim(substring(z.s, z.p2 + 1, z.p3 - z.p2))) end FirstNamefrom ( select s, datalength(s) - charindex(' ', reverse(s), charindex(',', reverse(s))) p1, charindex(',', s) p2, datalength(s) - charindex(' ', reverse(s)) p3, datalength(s) p4 from @s ) zPeter LarssonHelsingborg, Sweden |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-07-25 : 16:39:16
|
| U mean, u want each record to be splitted by comma and remove the right most word and concat the 2 with a comma in between?How to differentiate 2nd & 3rd formats if the last name has 2 partsie. what will happen to De Silva, HarryShould it be De,Harry ?Srinika |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2006-07-25 : 17:10:09
|
quote: Originally posted by Srinika U mean, u want each record to be splitted by comma and remove the right most word and concat the 2 with a comma in between?How to differentiate 2nd & 3rd formats if the last name has 2 partsie. what will happen to De Silva, HarryShould it be De,Harry ?Srinika
No, of course not. It should return 'De Silva,Harry'I only want to get rid of the MiddleInitial and Suffix.Peter,I am trying to work through your method, but I do not quite understand how to get MY FIELD name into there...any suggestions?My table Name is Physicians and field name is AdmittingPhysician.ThanksHC |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-26 : 06:30:19
|
The only way I see you distinguishing between 'De Silva, Harry' and 'Jones III, Bob' is by listing out suffixes.Here's my suggestion...--datadeclare @t table (x varchar(200))insert @t select 'Pitt III, Brad A'union all select 'Cruise, Tom'union all select 'Foster,Jodie'union all select 'De Silva III, Paul B'union all select 'De Silva, Fred C'union all select 'De Silva, John'union all select 'De Silva I, Sally D'union all select 'De Silva II, Sarah E'union all select 'De Silva II,Bob F'union all select 'Madonna'--calculationdeclare @suffix table (suffix varchar(10)) --this will need filling up with all possible suffixesinsert @suffix select 'I'union all select 'II'union all select 'III'select replace(x, ', ', ',') as x from ( select case when x like '% [A-Z]' then left(x, len(x) - 2) else x end as x --x with middle initial removed from ( select case when suffix is null then x else replace(x, ' ' + suffix + ',', ',') end as x --x with suffix removed from @t t left outer join @suffix s on patindex('% ' + suffix + ',%', x) > 0) a) b/*resultsx ----------------------Pitt,BradCruise,TomFoster,JodieDe Silva,PaulDe Silva,FredDe Silva,JohnDe Silva,SallyDe Silva,SarahDe Silva,BobMadonna*/Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-26 : 06:33:22
|
Oh, and for you to use this on your table, you can use this...declare @suffix table (suffix varchar(10)) --this will need filling up with all possible suffixesinsert @suffix select 'I'union all select 'II'union all select 'III'select replace(x, ', ', ',') as AdmittingPhysician from ( select case when x like '% [A-Z]' then left(x, len(x) - 2) else x end as x --x with middle initial removed from ( select case when suffix is null then AdmittingPhysician else replace(AdmittingPhysician, ' ' + suffix + ',', ',') end as x --x with suffix removed from Physicians t left outer join @suffix s on patindex('% ' + suffix + ',%', AdmittingPhysician) > 0) a) bRyan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-26 : 06:40:56
|
Absolutely nice solution Ryan! But what about names like 'Larsson, Peter Jr' ?? Would it be possible to put Jr/Sr in the suffix table too?Peter LarssonHelsingborg, Sweden |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-26 : 07:06:33
|
I've left that as an exercise to the reader... Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2006-07-26 : 09:12:57
|
| Thank you Ryan and Peter, I really appeciate that hand holding there. I will work through the suffixes. Thanks againHC |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-07-28 : 09:58:34
|
This function will do exactly what you want:You would call it like this:select dbo.FormatName([YourNameString], 'L, F')[Edited post today]CREATE function FormatName(@NameString varchar(100), @NameFormat varchar(20))returns varchar(100) asbegin--blindman, 11/04--blindman, 7/06: modified to account for suffixes trailing last name--FormatName decodes a NameString into its component parts and returns it in a requested format.--@NameString is the raw value to be parsed.--@NameFormat is a string that defines the output format. Each letter in the string represents--a component of the name in the order that it is to be returned.-- [H] = Full honorific-- [h] = Abbreviated honorific-- [F] = First name-- [f] = First initial-- [M] = Middle name-- [m] = Middle initial-- [L] = Last name-- [l] = Last initial-- [S] = Full suffix-- [s] = Abbreviated suffix-- [.] = Period-- [,] = Comma-- [ ] = Space--Test variables-- declare @NameString varchar(50)-- declare @NameFormat varchar(20)-- set @NameFormat = 'F L M S'-- set @NameString = 'Lindman II, Bruce A'Declare @Honorific varchar(20)Declare @FirstName varchar(20)Declare @MiddleName varchar(30)Declare @LastName varchar(30)Declare @Suffix varchar(20)Declare @TempString varchar(100)Declare @TempString2 varchar(100)Declare @IgnorePeriod char(1)--Prepare the string--Make sure each period is followed by a space character.set @NameString = rtrim(ltrim(replace(@NameString, '.', '. ')))--Eliminate double-spaces.while charindex(' ', @NameString) > 0 set @NameString = replace(@NameString, ' ', ' ')--Eliminate periodswhile charindex('.', @NameString) > 0 set @NameString = replace(@NameString, '.', '')--If the lastname is listed first, strip it off.set @TempString = rtrim(left(@NameString, charindex(' ', @NameString)))if @TempString in ('VAN', 'VON', 'MC', 'Mac', 'DE') set @TempString = rtrim(left(@NameString, charindex(' ', @NameString, len(@TempString)+2)))--Search for suffixes trailing the LastNameset @TempString2 = ltrim(right(@NameString, len(@NameString) - len(@TempString)))set @TempString2 = rtrim(left(@TempString2, charindex(' ', @TempString2)))if right(@TempString2, 1) = ',' begin set @Suffix = left(@TempString2, len(@TempString2)-1) set @LastName = left(@TempString, len(@TempString)) endif right(@TempString, 1) = ',' set @LastName = left(@TempString, len(@TempString)-1)if len(@LastName) > 0 set @NameString = ltrim(right(@NameString, len(@NameString) - len(@TempString)))if len(@Suffix) > 0 set @NameString = ltrim(right(@NameString, len(@NameString) - len(@TempString2)))--Get rid of any remaining commaswhile charindex(',', @NameString) > 0 set @NameString = replace(@NameString, ',', '')--Get Honorific and strip it out of the stringset @TempString = rtrim(left(@NameString, charindex(' ', @NameString + ' ')))if @TempString in ( 'Admiral', 'Adm', 'Captain', 'Cpt', 'Capt', 'Commander', 'Cmd', 'Corporal', 'Cpl', 'Doctor', 'Dr', 'Father', 'General', 'Gen', 'Honorable', 'Hon', 'Lieutenant', 'Lt', 'Madam', 'Mdm', 'Madame', 'Mme', 'Mademoiselle', 'Mlle', 'Major', 'Maj', 'Miss', 'Ms', 'Mr', 'Mrs', 'Private', 'Pvt', 'Professor', 'Prof', 'Rabbi', 'Reverend', 'Rev', 'Senior', 'Sr', 'Seniora', 'Sra', 'Seniorita', 'Srta', 'Sergeant', 'Sgt', 'Sir', 'Sister') set @Honorific = @TempStringif len(@Honorific) > 0 set @NameString = ltrim(right(@NameString, len(@NameString) - len(@TempString)))--Get Suffix and strip it out of the stringif @Suffix is null begin set @TempString = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' '))) if @TempString in ( 'Attorney', 'Att', 'Atty', 'BA', 'BS', 'DDS', 'DVM', 'Esquire', 'Esq', 'II', 'III', 'IV', 'Junior', 'Jr', 'MBA', 'MD', 'PHD', 'Senior', 'Sr') set @Suffix = @TempString if len(@Suffix) > 0 set @NameString = rtrim(left(@NameString, len(@NameString) - len(@TempString))) endif @LastName is nullbegin --Get LastName and strip it out of the string set @LastName = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' '))) set @NameString = rtrim(left(@NameString, len(@NameString) - len(@LastName))) --Check to see if the last name has two parts set @TempString = ltrim(right(@NameString, charindex(' ', Reverse(@NameString) + ' '))) if @TempString in ('VAN', 'VON', 'MC', 'Mac', 'DE') begin set @LastName = @TempString + ' ' + @LastName set @NameString = rtrim(left(@NameString, len(@NameString) - len(@TempString))) endend--Get FirstName and strip it out of the stringset @FirstName = rtrim(left(@NameString, charindex(' ', @NameString + ' ')))set @NameString = ltrim(right(@NameString, len(@NameString) - len(@FirstName)))--Anything remaining is MiddleNameset @MiddleName = @NameString--Create the output stringset @TempString = ''while len(@NameFormat) > 0begin if @IgnorePeriod = 'F' or left(@NameFormat, 1) <> '.' begin set @IgnorePeriod = 'F' set @TempString = @TempString + case ascii(left(@NameFormat, 1)) when '32' then case right(@TempString, 1) when ' ' then '' else ' ' end when '44' then case right(@TempString, 1) when ' ' then '' else ',' end when '46' then case right(@TempString, 1) when ' ' then '' else '.' end when '70' then isnull(@FirstName, '') when '72' then case @Honorific when 'Adm' then 'Admiral' when 'Capt' then 'Captain' when 'Cmd' then 'Commander' when 'Cpl' then 'Corporal' when 'Cpt' then 'Captain' when 'Dr' then 'Doctor' when 'Gen' then 'General' when 'Hon' then 'Honorable' when 'Lt' then 'Lieutenant' when 'Maj' then 'Major' when 'Mdm' then 'Madam' when 'Mlle' then 'Mademoiselle' when 'Mme' then 'Madame' when 'Ms' then 'Miss' when 'Prof' then 'Professor' when 'Pvt' then 'Private' when 'Sr' then 'Senior' when 'Sra' then 'Seniora' when 'Srta' then 'Seniorita' when 'Rev' then 'Reverend' when 'Sgt' then 'Sergeant' else isnull(@Honorific, '') end when '76' then isnull(@LastName, '') when '77' then isnull(@MiddleName, '') when '83' then case @Suffix when 'Att' then 'Attorney' when 'Atty' then 'Attorney' when 'Esq' then 'Esquire' when 'Jr' then 'Junior' when 'Sr' then 'Senior' else isnull(@Suffix, '') end when '102' then isnull(left(@FirstName, 1), '') when '104' then case @Honorific when 'Admiral' then 'Adm' when 'Captain' then 'Capt' when 'Commander' then 'Cmd' when 'Corporal' then 'Cpl' when 'Doctor' then 'Dr' when 'General' then 'Gen' when 'Honorable' then 'Hon' when 'Lieutenant' then 'Lt' when 'Madam' then 'Mdm' when 'Madame' then 'Mme' when 'Mademoiselle' then 'Mlle' when 'Major' then 'Maj' when 'Miss' then 'Ms' when 'Private' then 'Pvt' when 'Professor' then 'Prof' when 'Reverend' then 'Rev' when 'Senior' then 'Sr' when 'Seniora' then 'Sra' when 'Seniorita' then 'Srta' when 'Sergeant' then 'Sgt' else isnull(@Honorific, '') end when '108' then isnull(left(@LastName, 1), '') when '109' then isnull(left(@MiddleName, 1), '') when '115' then case @Suffix when 'Attorney' then 'Atty' when 'Esquire' then 'Esq' when 'Junior' then 'Jr' when 'Senior' then 'Sr' else isnull(@Suffix, '') end else '' end if ((ascii(left(@NameFormat, 1)) = 72 and @Honorific in ('Father', 'Rabbi', 'Sister')) or (ascii(left(@NameFormat, 1)) = 115 and @Suffix in ('BA', 'BS', 'DDS', 'DVM', 'II', 'III', 'IV', 'MBA', 'MD', 'PHD'))) set @IgnorePeriod = 'T' end set @NameFormat = right(@NameFormat, len(@NameFormat) - 1)end--select @TempStringReturn @TempStringend |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-28 : 11:12:08
|
Nice one blindman!Testing that on the silly sample data I supplied, it looks like we might need to replace ',' with ', ' before we call it (or modify to do this within the function), and it looks like we might need to modify slightly to cope with people like 'Madonna' or 'Pele'.These are trivial points though, and this is certainly a huge step in the right direction. Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-07-28 : 12:10:54
|
| I thought I covered commas without trailing spaces in the code.If anyone runs across any names that this formats incorrectly, please send them to me and I will try to adapt the code to them. I've been addint logic to this algorithm for nearly 10 years (hence the length...). Every update makes it more robust. |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-07-28 : 12:12:14
|
...and who wouldn't have trouble coping with Madonna? Let's try to set some realistic expectations. |
 |
|
|
|
|
|
|
|