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

后端 未结 10 1725
失恋的感觉
失恋的感觉 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: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

提交回复
热议问题