Link to home
Start Free TrialLog in
Avatar of Butterfly2
Butterfly2

asked on

Remove Middle Initial from Name field sql query

Good Afternoon Experts,

I have another question, I am using a name column in a query.  The name shows like this
Lastname, Firstname MI.  Not all fields have a middle initial, how would I trim the middle initial from the field?

select  c.episode_no,
           from smsmir.mir_pms_case

for example I have

Smith, John
Brown, Sandy L
Puz, Karen R
johnson,  Carl
Avatar of Simone B
Simone B
Flag of Canada image

This will find any row that has a space followed by one character at the end, and trim it. It assumes that the same pattern is followed for the rows that have an initial.

For the rows that don't have an initial, it will show the full name.

SELECT 
	CASE WHEN NameField LIKE '% _' THEN LEFT(NameField, LEN(NameField)-2)
	ELSE NameField END

Open in new window

Avatar of Lee
There are a few ways. Here's one a played with:

create table zzztest (irecid int identity (1, 1),
nvchname nvarchar(100))

insert into zzztest (nvchname) values('Smith, John')
insert into zzztest (nvchname) values('Brown, Sandy L')
insert into zzztest (nvchname) values('Puz, Karen R')
insert into zzztest (nvchname) values('johnson,  Carl')


select irecid,
       case len(ltrim(right(nvchname, 2)))
           when 1 then left(nvchname, len(nvchname) - 2)
           else nvchname
       end as nvchname
  from zzztest

Open in new window

select  left(rtrim(c.episode_no),len(c.episode_no) - case when c.episode_no like '%, _% _' then 2 else 0 end ) as c.episode_no
           from smsmir.mir_pms_case

the point is you should search for   lastname followed by blank and comma and blank then at least one character followed by a blank and a character before you attempt to trim off the "middle initial"

however you have to be very confident that all your data follows your perceived layout...

do you need to allow for multiple middle initials etc...
Avatar of Butterfly2
Butterfly2

ASKER

Both work, however I just noticed that there are records that have the whole middle name.  for example

not removing
lee, sharon emma  

it is removing
 lee, sharon e
With mine at least it would leave those instances alone, however you haven't said what you'd like to do with them.

If you encountered:

lee, sharon emma

Would you like it returned as:

lee, sharon

or left as the whole string?
If you wanted to remove the middle name or initial:

select irecid,
       case charindex(' ', replace(nvchname, ', ', ''), 1)
       when 0 then nvchname
       else replace(left(replace(nvchname, ', ', ','), charindex(' ', replace(nvchname, ', ', ','), 1)), ',', ', ')
       end as nvchname
from zzztest

Open in new window


There's probably a better way though :)
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial