简体   繁体   中英

MySQL: Return SUBSTR between specific delimiter and next non-unique delimiter

I'm trying to return a SUBSTR between two delimiters. The Problem is, that the second delimiter is non-unique in the whole string.

Example:

testurl.de/some:text/&[uniquedelimiter]=texttoextract:sometext:someothertext

So I want to extract the string between "&[uniquedelimiter]=" and the next ":" after the unique delimiter. The occurrence of ":" in the string is variable.

Right now I'm using a double SUBSTRING_INDEX function, but I get errors if the string contains more than one ":".

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(list_of_urls, '&[uniquedelimiter]=',-1),':')
FROM url_table;

I couldn't find a comparable solution on any Community.

Thank you so much for your help, Lars

This seems to be working, maybe you can sort it, not sure:

SELECT
SUBSTRING(
    url,
    (POSITION('&[uniquedelimiter]=' IN url) + LENGTH('&[uniquedelimiter]=')),
    LOCATE(':', url, (POSITION('&[uniquedelimiter]=' IN url) + LENGTH('&[uniquedelimiter]='))) -
    (POSITION('&[uniquedelimiter]=' IN url) + LENGTH('&[uniquedelimiter]='))
)
from bla

My strategy was like this:

SET @url                   = (SELECT url from bla limit 1);
SET @pos_uniq_deli_start   = POSITION('&[uniquedelimiter]=' IN @url);
SET @uniq_deli_length      = LENGTH('&[uniquedelimiter]=');
SET @pos_uniq_deli_end     = @pos_uniq_deli_start + @uniq_deli_length;
--                           LOCATE(char, field, offset)
SET @pos_colon_after_deli  = LOCATE(':', @url, @pos_uniq_deli_end);
SET @delta_unqi_del_colon  = @pos_colon_after_deli - @pos_uniq_deli_end;

--            SUBSTRING(field, start_pos, length)
SET @result = SUBSTRING(@url,@pos_uniq_deli_end, @delta_unqi_del_colon);
SELECT @result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM