MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Split Name Column into Firstname, Lastname, Middlename,sufx

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Mon Mar 19, 2012 12:48 pm    Post subject: Split Name Column into Firstname, Lastname, Middlename,sufx Reply with quote

Hi,
I have a table and one of the column is name with CHAR(25). The name is first name followed by Middle name and last name with comma separated and suffix at the end if there any. Not all the names have middle name or suffix. There is no fixed length for first name and last name. It has only separator comma between two. I need to extract data from this column to populate into another table with 4 different columns such as First Name(25), Middle Name(25), Last Name(25) and suffix(3). Is there anyway, Can I extract using SQL into different columns?

Eg.
LUDICK, TIMOTHY
BIDDLE, JOHN FRANKLIN
KORMANIK, PAUL S
WILFRED L POTTER
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Mon Mar 19, 2012 1:30 pm    Post subject: Reply with quote

This topic has been discussed before (within last year) and solutions provided.
suggest you search the db2 forum.

or you can wait and somebody may provide it to you.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Mon Mar 19, 2012 2:12 pm    Post subject: Reply with quote

My apologies, i searched and could not find it.

you have not given a sample column value that would generate the 4th column.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Mon Mar 19, 2012 2:22 pm    Post subject: Reply with quote

i would use a case statement predicated on finding a comma,
WHEN POSSTR(NAME,',') > 0 then your order is last name, first name, middle initial/name, title
otherwise you know it is first, middle, last, title.

you can use a combination of POSSTR (or LOCATE) and SUBSTR
to extract the different portions of the column

to find the second and third space, you can use POSSTR(SUBSTR(NAME,POSSTR(NAME,' ') + 1))
as a starting point.

is it easy, no, but you wanted the sql.

i personnally would unload, then parse with sort and then use the sort output to feed a cobol program to update the new table.

or simply select NAME,
parse with cobol UNSTRING
then update the 4 columns with the host variables.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Mar 19, 2012 6:29 pm    Post subject: Reply with quote

schintala,

Please post with a meaningful titles. A simple "sql help" isn't going to help. I edit the title. Also it would have been nice if you have posted the desired output from the above sample.

I haven't done the suffix part( or lack of data as you show), but here is a untested version for splitting the 25 byte column into 3 separate fields

Code:

SELECT REPLACE(A.FNAME,',',' ')                   AS FIRSTNAME   
      ,SUBSTR(A.MLNAME,1,LOCATE(' ',A.MLNAME)-1)  AS MIDDLENAME   
      ,SUBSTR(A.MLNAME,LOCATE(' ',A.MLNAME)+1,                   
       LENGTH(A.MLNAME) - LOCATE(' ',A.MLNAME))   AS LASTNAME     
  FROM (SELECT SUBSTR(NAMECOL,1,LOCATE(' ',NAMECOL)-1) AS FNAME   
              ,SUBSTR(NAMECOL,LOCATE(' ',NAMECOL)+1,             
              LENGTH(NAMECOL) - LOCATE(' ',NAMECOL))   AS MLNAME   
        FROM TABLE) A                                         
  ;     


The output would be
Code:


FIRSTNAME                  MIDDLENAME                 LASTNAME   
================================================================
LUDICK                     TIMOTHY                               
BIDDLE                     JOHN                       FRANKLIN   
KORMANIK                   PAUL                       S         
WILFRED                    L                          POTTER     
Back to top
View user's profile Send private message Send e-mail Visit poster's website
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Tue Mar 20, 2012 1:20 pm    Post subject: Reply with quote

Kolusu,
Thank you so much for your valuable update on the SQL. We have developed cobol program to read the data from the name column and inserted into other columns after splitting. As the data in name column is not consistent, we got to update data through program. Thank you for your suggestion and make sure I'll have meaningful title in future postings.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Tue Mar 20, 2012 2:35 pm    Post subject: Reply with quote

schintala,
thx for the follow-up.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group