Search code examples
sqlregexsnowflake-cloud-data-platformregexp-replace

Snowflake : REGEXP replace with uppercase of capture group


I want to replace the very first letter after a comma(,) with uppercase of it in snowflake database. Below given is what I tried, but it did not work.

eg:

Apple,ball,cat --> Apple,Ball,Cat
Bulb,LED,tube --> Bulb,LED,Tube
SELECT  REGEXP_REPLACE('Apple,ball,cat',',(\\\w)',UPPER('\\\1'));

,(\\\w) captures letters after the comma, but UPPER('\\\1') does not convert it to uppercase.


Solution

  • I am not sure if you can use functions inside REGEXP_REPLACE at all.

    Please use the built-in INITCAP function

    SELECT INITCAP('Apple,ball,cat', ',');
    

    Reference: INITCAP

    Or maybe like this:

    SELECT LISTAGG(UPPER(LEFT(VALUE, 1)) || SUBSTRING(VALUE, 2, LEN(VALUE)), ',')
      FROM TABLE(SPLIT_TO_TABLE('Apple,ball,cat', ',')) as t(val);