Solved

Remove Middle Initial from Name field sql query

Posted on 2014-01-03
7
1,860 Views
Last Modified: 2014-02-11
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
0
Comment
Question by:Butterfly2
7 Comments
 
LVL 11

Expert Comment

by:Simone B
ID: 39754200
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

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39754216
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

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 39754273
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...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Butterfly2
ID: 39754274
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
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39754310
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?
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39754356
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 :)
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 330 total points
ID: 39760321
You can remove the RTRIMs below if the name will never have trailing spaces:


SELECT
    LEFT(name_column, LEN(name_column) -
        CASE WHEN RTRIM(name_column) LIKE '%[^ ] %[^ ] %'
        THEN CHARINDEX(' ', REVERSE(RTRIM(name_column))) ELSE 0 END) AS final_name
FROM (
    SELECT 'Smith, John' AS name_column UNION ALL
    SELECT 'Brown, Sandy L' UNION ALL
    SELECT 'Puz, Karen R' UNION ALL
    SELECT 'johnson,  Carl' UNION ALL
    SELECT 'lee, sharon emma'
) AS test_data
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now