Solved

Remove Middle Initial from Name field sql query

Posted on 2014-01-03
7
2,115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

738 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