Home > Blockchain >  REGEXP_LIKE Oracle equivalent to count characters in Snowflake
REGEXP_LIKE Oracle equivalent to count characters in Snowflake

Time:03-15

I am trying to come up with an equivalent of the below Oracle statement in Snowflake. This would check if the different parts of the string separated by '.' matches the number of characters in the REGEXP_LIKE expression. I have come up with a rudimentary version to perform the check in Snowflake but I am sure there's a better and cleaner way to do it. I am looking to come up with a one-liner regular expression check in Snowflake similar to Oracle. Appreciate your help!

-- Oracle
SELECT -- would return True
    CASE
        WHEN REGEXP_LIKE('AB.XYX.12.34.5670.89', '^\w{2}\.\w{3}\.\w{2}') THEN 'True'
       ELSE NULL
    END AS abc
    FROM DUAL

-- Snowflake
SELECT -- would return True
    REGEXP_LIKE(SPLIT_PART('AB.XYX.12.34.5670.89', '.', 1), '[A-Z0-9]{2}') AND
    REGEXP_LIKE(SPLIT_PART('AB.XYX.12.34.5670.89', '.', 2), '[A-Z0-9]{3}') AND
    REGEXP_LIKE(SPLIT_PART('AB.XYX.12.34.5670.89', '.', 3), '[A-Z0-9]{2}') AS abc

CodePudding user response:

You need to add a .* at the end as the REGEXP_LIKE adds explicit ^ && $ to string:

The function implicitly anchors a pattern at both ends (i.e. '' automatically becomes '^$', and 'ABC' automatically becomes '^ABC$'). To match any string starting with ABC, the pattern would be 'ABC.*'.

select 
    column1 as str,
    REGEXP_LIKE(str, '\\w{2}\\.\\w{3}\\.\\w{2}.*') as oracle_way
FROM VALUES 
    ('AB.XYX.12.34.5670.89')
    ;

gives:

STR ORACLE_WAY
AB.XYX.12.34.5670.89 TRUE

Or in the context of your question:

SELECT IFF(REGEXP_LIKE('AB.XYX.12.34.5670.89', '\\w{2}\\.\\w{3}\\.\\w{2}.*'), 'True', null) AS abc;
  • Related