Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove text at the beginning and end of a field with a non-standard middle

Posted on 2012-03-22
3
Medium Priority
?
371 Views
Last Modified: 2012-03-26
I'm working on a report that needs to make use of a field that contains the following text:

CTMR 7.3   John Smith                   212845
CTMR 2.2   Jane Smith                   212845
GRPV 7.3   John Doe                   212845
MEMP 2.2   Jane Doe                   212845

The only part of this field I need is the first and last names in the middle but they are of non-standard length and vary with every employee.  The text to the left of the name is a system environment and the numbers are the far right are an identifier unique to them.

How would I go about stripping this field down to just say John Smith, etc?

Thanks in advance for any assistance you can provide,

~SPAR
0
Comment
Question by:StrawsPulledAtRandom
[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
  • 2
3 Comments
 
LVL 101

Assisted Solution

by:mlmcc
mlmcc earned 668 total points
ID: 37754826
Assuming you always have

XXXX XXX First Last     XXXXXXX

The X's can be any length for this to work, just need to ensure there is 1 space between the pieces.

Local StringVar Array TextList;
TextList := Split({YourFIeld},' ');
TextList[3] & ' ' & TextList[4]

If the spacing is not fixed then another approach will be needed
XXXX XXX First Last     XXXXXXX
XXXXXX XXX First Last     XXXXXXX
XXXX XXXXX First Last     XXXXXXX

All those work.  However
XXXX   XXX First Last     XXXXXXX
XXXX XXX   First Last     XXXXXXX

Will require other methods.

mlmcc
0
 
LVL 59

Assisted Solution

by:HainKurt
HainKurt earned 1332 total points
ID: 37755035
maybe this work for you:

create a function like this:

ALTER FUNCTION dbo.SplitN(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (n int, item varchar(8000))       
as       
begin       
    declare @ix int = 0;     
    declare @idx int = 1;       
    declare @slice varchar(8000);       
      
    if len(@String)<1 or @String is null  return;       
      
    while @idx!= 0       
    begin     
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1);       
        else       
            set @slice = @String;      
          
        if(len(@slice)>0)
        begin  
            set @ix=@ix+1;
            insert into @temptable(n,Item) values(@ix, @slice);
        end;  
  
        set @String = right(@String,len(@String) - @idx);      
        if len(@String) = 0 break;
    end   
return       
end  

Open in new window


based on the code: http://blog.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx

then use this

with t as (
select 'CTMR 7.3   John Smith                   212845' data 
union select 'CTMR 2.2   Jane Smith                   212845'
union select 'GRPV 7.3   John Doe                   212845'
union select 'MEMP 2.2   Jane Doe                   212845'
)
select  
  (select item from dbo.splitn(data, ' ') where n=3) + ' ' + (select item from dbo.splitn(data, ' ') where n=4) FullName
from t 

FullName
John Smith
Jane Smith
John Doe
Jane Doe

Open in new window

0
 
LVL 59

Accepted Solution

by:
HainKurt earned 1332 total points
ID: 37755057
or we can create another function based on above function

create function SplitNGetN(@String varchar(8000), @Delimiter char(1), @ix int =1) returns varchar(8000) as
begin
  declare @result varchar(8000) = '';
  set @result = (select item from dbo.splitn(@String, @Delimiter) where n=@ix)
  return @result;
end;

Open in new window


this function uses the function @ previous post

then our code will be much beautiful :)

with t as (
select 'CTMR 7.3   John Smith                   212845' data 
union select 'CTMR 2.2   Jane Smith                   212845'
union select 'GRPV 7.3   John Doe                   212845'
union select 'MEMP 2.2   Jane Doe                   212845'
)
select dbo.SplitNGetN(data, ' ', 3) + ' ' + dbo.SplitNGetN(data, ' ', 4) FullName from t

FullName
Jane Smith
John Smith
John Doe
Jane Doe

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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