如何删除雪花 sql 中特定特殊字符后的字符串中的字符?

How can I remove characters in a string after a specific special character in snowflake sql?

我的数据库中有一个 url 列。我想删除 ? 之后的所有字符.

select regexp_replace(page_url,'?(.*)', '') as clean_url from demo

但我收到以下错误: 无效的正则表达式:'?(.*)',没有重复运算符的参数:?

使用 LEFTCHARINDEX:

SELECT IFF(CHARINDEX('?', page_url)>0,
           LEFT(page_url, CHARINDEX('?', page_url)),
           page_url) AS clean_url 
FROM demo

? 是重复先验标记的正则表达式匹配标记,因此要显式匹配它需要对其进行转义:\?

as per the doc的:

To escape meta-characters (as mandated by the POSIX standard). For example, to force the regular expression meta-characters * and ? to be treated as literals rather than as meta-characters, use \* and \?.

但是如果您通过 SQL 解析器输入它,则需要对其进行双重转义。因此需要 \?

select
    page_url
    ,regexp_replace(page_url,'\?(.*)', '') as clean_url 
from values
    ('https://example.com/page.html?parameter1')
    t(page_url);

给出:

PAGE_URL CLEAN_URL
https://example.com/page.html?parameter1 https://example.com/page.html