Search code examples
sqlstringcasesql-likesnowflake-cloud-data-platform

If string starts with 8 characters with a dash "-" insert specific label


In SQL (specifically in Snowflake), I have a string that looks like this:

Col_A
afdafe12-123-42141
jkk121jd-313-129en
dje-332-djak

How do I make a rule where if the pattern begins with 8 characters with a "-" it is labeled as "keep"?

I want to do a case when statement similar to below:

case when Col_A "regex rule" then 'keep' 
when Col_A like 'dje%' then 'remove' else 'disregard' end new_col
from dataframe

Solution

  • You may use 8 underscores. An underscore matches a single character

    case when Col_A like '________-%' then 'keep' 
         when Col_A like 'dje%' then 'remove' 
         else 'disregard' end as new_col
    

    Tip: Snowflake also supports ilike which is a case insensitive like. You can swap it out if appropriate