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
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
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
select left(rtrim(c.episode_no),l en(c.episo de_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...
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...
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
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 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:
There's probably a better way though :)
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
There's probably a better way though :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the rows that don't have an initial, it will show the full name.
Open in new window