Solved

Remove Middle Initial from Name field sql query

Posted on 2014-01-03
7
1,913 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

867 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

18 Experts available now in Live!

Get 1:1 Help Now