Search code examples
splitdb2

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


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 result in two values:

CHG 
FFH. 

I tried using split function but it is not a function in DB2.

Any help will be appreciated.


Solution

  • 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 '%-%'