Joined: 18 May 2005 Posts: 108 Topics: 31 Location: USA
Posted: Mon Mar 19, 2012 12:48 pm Post subject: Split Name Column into Firstname, Lastname, Middlename,sufx
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
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
Posted: Mon Mar 19, 2012 2:22 pm Post subject:
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
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
Posted: Mon Mar 19, 2012 6:29 pm Post subject:
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
Joined: 18 May 2005 Posts: 108 Topics: 31 Location: USA
Posted: Tue Mar 20, 2012 1:20 pm Post subject:
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.
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