Hi All,
I grew tired of typing this code into my queries (after the second time!), so I created a function today to trim suffixes off of last name and full name fields. I am sure there are better ways to do this, but this will help me tremendously. Hopefully it will save someone else out there the pain I went through.
If anyone has a better way, I am all ears!
Alex
It's a magical time of year in Philadelphia. Eagles training camp marks the end of another brutal season of complaining about the Phillies.
I grew tired of typing this code into my queries (after the second time!), so I created a function today to trim suffixes off of last name and full name fields. I am sure there are better ways to do this, but this will help me tremendously. Hopefully it will save someone else out there the pain I went through.
Code:
Create Function fnNameTrim(@name varchar (100))
Returns varchar(100)
AS
Begin
Declare @fixname varchar(100)
set @fixname =
(CASE WHEN right((@name),5) in (', Jr.', ', Sr.', ', III', ', 3rd') then
ltrim(left(@name, len(@name) - 5))
WHEN right(@name,4) in (' Jr.', ' Sr.', ' III', ' 3rd', ', II', ', IV', ', MA', ', MD') then
ltrim(left(@name, len(@name) - 4))
WHEN right(@name,3) in (' Jr', ' Sr', ' II', ' IV', ' I.', ' J.') then
ltrim(left(@name, len(@name) - 3))
WHEN right(@name,1) = '.' and right(@name, 3) NOT IN ('Jr.', 'Sr.') then
ltrim(left(@name, len(@name) - 1))
ELSE (@name) END)
return ltrim(rtrim(@fixname))
End
If anyone has a better way, I am all ears!
Alex
It's a magical time of year in Philadelphia. Eagles training camp marks the end of another brutal season of complaining about the Phillies.