How do I extract text from string between underscores with mysql?
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)