简体   繁体   中英

Parse 1 column into 3 columns using DB2 SQL

Using DB2 for z/OS 10, I'm have a column (ADDRESS3) in a table that contains city, state, and zipcode all in one column. But, the formatting is somewhat "free-form". (see code below)

I can successfully parse the city by taking everything before the comma. Using only DB2 SQL, how would I parse out the state and zipcode?

SELECT ADDRESS3,
       TRIM(SUBSTR(ADDRESS3,1,LOCATE(',', ADDRESS3)-1)) AS CITY
  FROM MYTABLE

ADDRESS3                            CITY         
----------------------------------  -------------
GRANADA HILLS       ,CA 91344       GRANADA HILLS
SIMI VALLEY         ,CA 93065       SIMI VALLEY  
BUENA PARK          ,CA 90621       BUENA PARK   
SHERMAN OAKS        ,CA 91423       SHERMAN OAKS 
GLENDALE            ,CA 91203-2089  GLENDALE     
VENTURA             ,CA 93002       VENTURA      
HAWTHORNE           ,CA 90250       HAWTHORNE    
PASADENA            ,CA 91185-2594  PASADENA     
BEVERLY HILLS       ,CA 90211       BEVERLY HILLS
ARCADIA             ,CA 91007       ARCADIA      
WALNUT CREEK        ,CA 94596       WALNUT CREEK 
INDEPENDANCE        ,CA 93526       INDEPENDANCE 
LOS ANGELES         ,CA 90017       LOS ANGELES  

Ultimately, I'd like the query results to look like this:

CITY          STATE ZIP
------------- ----- -----
GRANADA HILLS CA    91344
SIMI VALLEY   CA    93065
BUENA PARK    CA    90621
SHERMAN OAKS  CA    91423
GLENDALE      CA    91203
VENTURA       CA    93002
HAWTHORNE     CA    90250
PASADENA      CA    91185
...

Thanks!

Based on your code, if the state is 2 chars and zip 5 chars:

SELECT 
  ADDRESS3,
  TRIM(SUBSTR(ADDRESS3, 1, LOCATE(',', ADDRESS3)-1)) AS CITY,
  TRIM(SUBSTR(ADDRESS3, LOCATE(',', ADDRESS3) + 1), 2) AS STATE,
  TRIM(SUBSTR(ADDRESS3, LOCATE(',', ADDRESS3) + 4), 5) AS ZIP
FROM MYTABLE

I think you need this assuming the format is the same always.

  SELECT 
       TRIM(
         SUBSTR(
           SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1)
           , 1
           , POSITION(' ' , SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1))
         )
       ) AS STATE
    ,  TRIM(
           SUBSTR(
             SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1)
           , POSITION(' ' , SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1))
         ) 
       ) AS ZIP
  FROM MYTABLE

The demo result into these results with these values

Test data

  VALUES
    ('GRANADA HILLS       ,CA 91344')
  , ('SIMI VALLEY         ,CA 93065') 
  , ('GLENDALE            ,CA 91203-2089')
  , ('SIMI VALLEY         ,CA    93065')  
  , ('SIMI VALLEY         ,CA    93065') 
  , ('GLENDALE            ,CA    91203-2089') 

Results

STATE   ZIP
CA      91344
CA      93065
CA      91203-2089
CA      93065
CA      93065
CA      91203-2089

see demo

interestingly, this worked, too.

SELECT TRIM(SUBSTR(ADDRESS3, 1, locate(',', ADDRESS3) - 1)) AS city,
       SUBSTR(TRIM(SUBSTR(ADDRESS3, locate(',', ADDRESS3) + 1)), 1, 2) state,
       SUBSTR(TRIM(SUBSTR(TRIM(SUBSTR(ADDRESS3, locate(',', ADDRESS3) + 1)), 3)), 1, 5) zip
  FROM MyTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM