doubi1931 2015-08-17 14:18
浏览 113
已采纳

MySQL:在特定分隔符和下一个非唯一分隔符之间返回SUBSTR

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

  • 写回答

1条回答 默认 最新

  • duanfu1945 2015-08-17 14:32
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog