How to split a string value based on a delimiter in DB2

后端 未结 10 1601
失恋的感觉
失恋的感觉 2020-11-27 08:09

How do you split a string value in DB2?

For example, given the value:

CHG-FFH.

I want to split on the dash (-), which would resul

相关标签:
10条回答
  • 2020-11-27 08:18

    This is what i tried and it fetched me effective result. Hence sharing with all.

    select column_name, substr(column_name,1,locate('-',column_name)-1), 
    substr(column_name,locate('-',column_name)+1,
    length(substr(column_name,locate('-',column_name)+1))) from 
    table_name where column_name is not null and column_name!='' 
    and column_name like '%-%'
    
    0 讨论(0)
  • 2020-11-27 08:25

    If you are sure that each substrings are 3 characters long you can try this code, provided that TABLE1 is a table where there is at least X rows (X = 10 in this example):

    select rc, substr(string_to_split, (rc-1)*3+rc, 3) as result from
        (select row_number() over() as rc from TABLE1 fetch first 10 rows only) TB_rowcount
        cross join
        (select 'CHG-FFH' as string_to_split from sysibm.sysdummy1) T2
        where substr(string_to_split, (rc-1)*3+rc, 3) <> '   '
    

    If the length of the substrings are not the same you have to apply LOCATE function to find the separator

    0 讨论(0)
  • 2020-11-27 08:26

    The reason of so late answer is to show much more simple and universal way to achieve the goal. It's based on functions using regular expressions available even at the time, when the question was asked.

    To get N-th token (2-nd in the example):

    SELECT 
      COL
    -- since 9.7 (2009)
    , xmlcast(xmlquery('fn:tokenize($s, "-")[2]' passing COL as "s") as varchar(20)) as one
    -- since 11.1
    , REGEXP_SUBSTR(COL, '([^-]*)-?', 1, 2, '', 1) as two
    FROM (VALUES 'CHG-FFH.', 'ABC-DEF-GH') TAB (COL);
    

    The result is:

    |COL       |ONE                 |TWO       |
    |----------|--------------------|----------|
    |CHG-FFH.  |FFH.                |FFH.      |
    |ABC-DEF-GH|DEF                 |DEF       |
    

    To tokenize a string:

    -- since 9.7 (2009)
    SELECT TAB.COL, TOK.SEQ, TOK.TOKEN
    FROM 
      (VALUES 'CHG-FFH.', 'ABC-DEF-GH') TAB (COL)
    , XMLTABLE
    (
      'for $id in tokenize($s, "-") return <i>{string($id)}</i>' PASSING TAB.COL AS "s"
      COLUMNS
        SEQ   FOR ORDINALITY
      , TOKEN VARCHAR(20) PATH '.'
    ) TOK
    ORDER BY TAB.COL, TOK.SEQ;
    

    The result is:

    |COL       |SEQ                 |TOKEN               |
    |----------|--------------------|--------------------|
    |ABC-DEF-GH|1                   |ABC                 |
    |ABC-DEF-GH|2                   |DEF                 |
    |ABC-DEF-GH|3                   |GH                  |
    |CHG-FFH.  |1                   |CHG                 |
    |CHG-FFH.  |2                   |FFH.                |
    
    0 讨论(0)
  • 2020-11-27 08:26

    This answer is not totally different, but it impements LOCATE_IN_STRING in a more flexible way which is useful, for example, if you have restrictions not to use functions.

    To get the nth string delimited by the character '-' as in your example:

    SUBSTR(THESTRING, LOCATE_IN_STRING(THESTRING, '-', 1,  n - 1) + 1, 
    LOCATE_IN_STRING(THESTRING||'-', '-', 1,  n) - LOCATE_IN_STRING(THESTRING, '-', 1,  n - 1) - 1) 
                            
    

    If you replace each occurrence of n by the column number you want, this construct will return the nth column's contents.


    Some explanation:

    The length of the string to be extracted by SUBSTR is calcluated from the difference between the occurrence of the nth and the (n + 1)th delimiter character (- in this case). Note that the (n + 1)th delimiter position is calculated from THESTRING plus an extra delimiter attached to its end to account for the case when we look for the last column and THESTRING does not end in a delimiter.

    0 讨论(0)
  • 2020-11-27 08:28

    Try this statement:

    select substr(your_value, 1,3), substr(your_value, 4, 3) from your_table
    
    0 讨论(0)
  • 2020-11-27 08:28

    In DB2

    SELECT
    '11,222,33,444' AS THE_F_STRING
    , SUBSTR('11,222,33,444', 1, LOCATE_IN_STRING('11,222,33,444',',',1,1)-1) AS AA
    , SUBSTR('11,222,33,444', LOCATE_IN_STRING('11,222,33,444',',',1,1)+1, LOCATE_IN_STRING('11,222,33,444',',',1,2)-LOCATE_IN_STRING('11,222,33,444',',',1,1)-1) AS BB 
    , SUBSTR('11,222,33,444', LOCATE_IN_STRING('11,222,33,444',',',1,2)+1, LOCATE_IN_STRING('11,222,33,444',',',1,3)-LOCATE_IN_STRING('11,222,33,444',',',1,2)-1) AS CC
    , SUBSTR('11,222,33,444', LOCATE_IN_STRING('11,222,33,444',',',1,3)+1, LENGTH('11,222,33,444')-LOCATE_IN_STRING('11,222,33,444',',',1,3)) AS DD
    FROM SYSIBM.SYSDUMMY1;
    

    Keep extrapolating...enjoy...

    0 讨论(0)
提交回复
热议问题