Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STRING MANIPULATIONS 1

Status
Not open for further replies.

cristi22

MIS
Aug 17, 2005
155
US
Hello All -

Please help with my string ordeals...
Need to separate the alpha:

Alpha =
LastName,FirstName,MiddleName

Data:
SANDERS,JOHNNIE
TORRES,DAISY,C

Results:
LAST_NAME MI FIRST_NAME
SANDERS JOHNNIE
TORES C DAISY

Missing the code for MI & the First should not have the MI

Code:
  SUBSTR(alpha,1,CASE                          
                    WHEN POSSTR(alpha,',') = 0   
                      THEN LENGTH(RTRIM(alpha))-2
                    ELSE POSSTR(alpha,',')-1   
                END )as last_name 
,SUBSTR(alpha,CASE                          
                    WHEN POSSTR(alpha,',') = 0   
                      THEN LENGTH(RTRIM(alpha))-2
                      ELSE POSSTR(alpha,',')+1   
                    END,LENGTH(RTRIM(alpha)) )    as first_name

Your help is really appreciated!!!
cristi
 
No DB2 instance at hand, so this is all theoretical without testing:

The position of the first seperator is:

Code:
POSSTR(FIELD,',')

The second one:

Code:
LOCATE(',' ,FIELD,(POSSTR(FIELD,',')+1))

These are absolute values (counting from start of the string)

The first substring:

Code:
SUBSTR(FIELD,1,POSSTR(FIELD,',')-1)

second substring:

Code:
SUBSTR(FIELD,POSSTR(FIELD,',')+1,(LOCATE(',' ,FIELD,(POSSTR(FIELD,',')+1))-POSSTR(FIELD,',')))

third substring:

Code:
SUBSTR(FIELD,LOCATE(',' ,FIELD,(POSSTR(FIELD,',')+1)),(LENGTH(FIELD)-LOCATE(',' ,FIELD,(POSSTR(FIELD,',')+1))))

This will probably work (some adjusting) if 2 seperators are present within the string.

If this is not the case then you need to work in the CASE construction like:

Code:
CASE WHEN 
LOCATE(',' ,FIELD,(POSSTR(FIELD,',')+1)) = 0 
THEN
<< scenario for just one seperator>>
ELSE
<< scenario for 2 seperators >>
END




Ties Blom

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top