Search code examples
db2substr

DB2 extract data between two delimiters


Here is the string I am trying to extract from: 'cn=xyxyxyxyxyx ousy,ou=information services,ou=domain users,dc=corp,dc=xyxyxx,dc=com'

I am trying to extract the string between the first 'ou=' and the second comma. In this case that is 'information services'

Here is what I have so far: SUBSTR(F_DN, locate('ou=', F_DN)+3, locate(',', F_DN, locate(',', F_DN)+1)+1 ) as role And this is the result: 'information services,ou=domain users,dc=co'

It seems to locate to the first character just fine but I cannot get the length correct.


Solution

  • Try this:

    select regexp_substr(str, 'ou=([^,]+)', 1, 1, '', 1)
    from (values 'cn=xyxyxyxyxyx ousy,ou=information services,ou=domain users,dc=corp,dc=xyxyxx,dc=com') t (str);