Solved

Remove Middle Initial from Name field sql query

Posted on 2014-01-03
7
1,954 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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:
Scott Pletcher 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

775 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