How do I extract text from string between underscores with mysql? I need to derive a column based on text from another column. 
For instance, in the 'channel' column I have this text:

"RbkSEM_google_ecom-_-Branded-_-Core-_-Realdock"


I need to pull out "Branded" and put it into a column called 'derived_channel'

The positioning of the text I need to pull out will always be after the 3rd underscore and before the 4th underscore. 

Thanks for help!
If you have the following:


table is called mytable
column channel has RbkSEM_google_ecom--Branded--Core-_-Realdock
you want to pull out Branded from between the -- tokens
place result in derived_channel


Here is your query

UPDATE mytable SET derived_channel = 
LEFT(SUBSTR(channel,LOCATE('--',channel)+LENGTH('--')),LOCATE('--',SUBSTR(channel,LOCATE('--',channel)+LENGTH('--'))) - 1);


Here is an example using user-defined variables

SET @st = 'RbkSEM_google_ecom--Branded--Core-_-Realdock';
SET @dm = '--';
SET @rv = LEFT(SUBSTR(@st,LOCATE(@dm,@st)+LENGTH(@dm)),LOCATE(@dm,SUBSTR(@st,LOCATE(@dm,@st)+LENGTH(@dm))) - 1);
SELECT @rv;


Here is the example executed

mysql&gt; SET @st = 'RbkSEM_google_ecom--Branded--Core-_-Realdock';
Query OK, 0 rows affected (0.00 sec)

mysql&gt; SET @dm = '--';
Query OK, 0 rows affected (0.00 sec)

mysql&gt; SET @rv = LEFT(SUBSTR(@st,LOCATE(@dm,@st)+LENGTH(@dm)),LOCATE(@dm,SUBSTR(@st,LOCATE(@dm,@st)+LENGTH(@dm))) - 1);
Query OK, 0 rows affected (0.00 sec)

mysql&gt; SELECT @rv;
+---------+
| @rv     |
+---------+
| Branded |
+---------+
1 row in set (0.00 sec)