【问题标题】:SQL, ORACLE - trim right string (remove all parameters from URL)SQL、ORACLE - 修剪正确的字符串(从 URL 中删除所有参数)
【发布时间】:2015-11-11 11:28:11
【问题描述】:

我查询带有 URL 的列。这些 URL 来自不同的来源并具有不同的格式。其中一些有参数。我希望查询此列并从第一个参数符号中右修剪 URL。

示例网址:

URLs
http://www.domain1.com/path/page?parameters1&parameters2
https://www.domain2.com/path/page?parameters1&parameters2/somemorestufftoscrape
domain3.com/path/page?parameters1&parameters2
http://www.domain4.com/path/page&parameters1?parameters2
https://www.domain5.com/path/noparametershere.html
domain6.com/path/page=?parameters1&parameters2

我想从 ?,&,=(代表我的案例参数的字符列表)中删除所有内容。

期望的输出:

TrimmedURLs
http://www.domain1.com/path/page
https://www.domain2.com/path/page
domain3.com/path/page
http://www.domain4.com/path/page
https://www.domain5.com/path
domain6.com/path/page

我尝试使用RTRIM如下:

select
   URLs
   rtrim(URLs, '?=&') as TrimmedURLs
from
   MyTable;

查询返回,但 URLs 列等于 TrimmedURLs(我做错了吗?)。

我尝试使用regexp_substr,但在有多个参数包租者的情况下,它会从最后一个而不是第一个修剪(参见页面中的第一个注释)。

  1. 要查询什么结果?
  2. 为什么 RTRIM 对我不起作用?

服务器是 Oracle 11g 网址类型为VARCHAR2(1024)

谢谢!

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

    REGEXP_SUBSTR() 听起来像是在这里使用的东西:

    with sample_data as (select 'http://www.domain1.com/path/page?parameters1&parameters2' url from dual union all
                         select 'https://www.domain2.com/path/page?parameters1&parameters2/somemorestufftoscrape' url from dual union all
                         select 'domain3.com/path/page?parameters1&parameters2' url from dual union all
                         select 'http://www.domain4.com/path/page&parameters1?parameters2' url from dual union all
                         select 'https://www.domain5.com/path/noparametershere.html' url from dual union all
                         select 'domain6.com/path/page=?parameters1&parameters2' url from dual)
    select url,
           regexp_substr(url, '[^?&=]+', 1, 1) main_url
    from   sample_data;
    
    URL                                                                             MAIN_URL                                                    
    ------------------------------------------------------------------------------- ------------------------------------------------------------
    http://www.domain1.com/path/page?parameters1&parameters2                        http://www.domain1.com/path/page                            
    https://www.domain2.com/path/page?parameters1&parameters2/somemorestufftoscrape https://www.domain2.com/path/page                           
    domain3.com/path/page?parameters1&parameters2                                   domain3.com/path/page                                       
    http://www.domain4.com/path/page&parameters1?parameters2                        http://www.domain4.com/path/page                            
    https://www.domain5.com/path/noparametershere.html                              https://www.domain5.com/path/noparametershere.html          
    domain6.com/path/page=?parameters1&parameters2                                  domain6.com/path/page 
    

    【讨论】:

    • 非常感谢,成功了。你知道我的 rtrim() 方法出了什么问题吗?
    • @Oren From the documentation: RTRIM removes from the right end of char all of the characters that appear in set. 所以,如果你有一个像 abc?&??= 这样的字符串,你的 rtrim 会返回 abc。但是,如果您的字符串类似于 abc?&??=z,则在字符串的右手端没有要删除的 ?、= 或 & 字符(最后一个字符是 z)。你所追求的是字符串的一部分,直到第一个 ?、= 或 & 字符,这意味着你在一个子字符串之后。希望这能让你更清楚?
    • @Gary_W 这取决于您使用的 OP 要求的哪一部分 - 如果您从“我想修剪所有内容到 ?、= 和 & 字符的右侧”,那么它可以工作。如果你从预期的输出中走出来,你是对的,我的查询结果不匹配。但是,我从 OP 所说的出发,并假设他们对 domain5 的预期输出是一个错字。
    【解决方案2】:

    如果你不喜欢regexp,你也可以使用substrinstr函数的组合:

    with sample_data as (select 'http://www.domain1.com/path/page?parameters1&parameters2' url from dual union all
                     select 'https://www.domain2.com/path/page?parameters1&parameters2/somemorestufftoscrape' url from dual union all
                     select 'domain3.com/path/page?parameters1&parameters2' url from dual union all
                     select 'http://www.domain4.com/path/page&parameters1?parameters2' url from dual union all
                     select 'https://www.domain5.com/path/noparametershere.html' url from dual union all
                     select 'domain6.com/path/page=?parameters1&parameters2' url from dual)
    select 
        url,
        substr(url, 0, instr(url,'?')-1) main_url
    from 
        sample_data
    

    【讨论】:

    • 当 URL 没有参数时,此选项不起作用。另外询问了 3 种类型的参数:&=?
    • 好吧,我没注意这一点。很好,第一个解决方案有效。
    猜你喜欢
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2011-07-27
    相关资源
    最近更新 更多