Home > Enterprise >  Regex expression from SQL to Snowflake [^''a-Z -]
Regex expression from SQL to Snowflake [^''a-Z -]

Time:01-22

The following code has to be converted to Snowflake SQL. I'm not sure how to convert the regex and also want to know if it means to negate whitespaces, alphabets and hyphen?

----SQL CODE

UPDATE t1 ASET A.col1 = 'NOT VALID'
FROM t1
INNER JOIN t2 B
ON A.ID=B.ID
WHEREB.name LIKE '%[^''a-Z -]%';

This is what I tried but no idea on regex.

UPDATE t1 A
SET A.col1 = 'NOT VALID' 
FROM t2 B
where A.ID=B.ID  AND
      RLIKE (B.name, [^''a-Z -]);   

CodePudding user response:

Some issues:

  • "Alphabets" are not matched by a-Z (actually, nothing is). That should be A-Za-z.
  • The pattern is implicitly "anchored" at both sides, meaning that the whole input has to match the pattern; not just a substring. You should add .* before and after the negative character class, or use another function, like regexp_instr
  • a regex is a string in Snowflake, so you should wrap it in quotes

Correction:

RLIKE (B.name, '.*[^''A-Za-z -].*')

Alternatively, you could use REGEXP_INSTR

REGEXP_INSTR (B.name, '[^''A-Za-z -]') > 0
  • Related