Skip to content
Advertisement

Selecting substring with different starting location

(I’m not sure if I’m using the correct title… please let me know if you think it should be changed to something else)

Hi, I’m trying to select substring with different starting location.

I have a table like this,

Req:


I’m trying to get a table like this:

After googling around, I found CHARINDEX() might be a solution. So I tried DB2 equivalent LOCATE():

The above query would give me a result like this:

This is okay for one major. What if I want all majors? I also tried using a host variable in LOCATE() and in LIKE clause. But it raised an error:

I’m not sure if the error was caused by my syntax or other restrictions.

Is there any way or workaround to accomplish this? Please let me know if you need more information. Thanks!

Advertisement

Answer

Db2 for LUW

DB2 for all pratforms using the REGEXP_SUBSTR function

DB2 for all pratforms using the LOCATE function

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement