Home > Back-end >  How can I remove characters in a string after a specific special character in snowflake sql?
How can I remove characters in a string after a specific special character in snowflake sql?

Time:03-31

I have a url column in my database. I want to remove all characters after ? .

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

But I'm getting the following error: Invalid regular expression: '?(.*)', no argument for repetition operator: ?

CodePudding user response:

Using LEFT and CHARINDEX:

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

CodePudding user response:

? is a regular expression match token to a repeat prior token and thus to match it explicitly it needs to be escaped: \?

as per the doc's:

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 \?.

BUT if you are entering it via the SQL parser, it will need to be double escaped. thus need to be \\?

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

gives:

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