Snowflake - replace selective double quotes

Viewed 947

I have data in a table with one column (varchar) that contains the following -

COL_A
-----
"trying to remove "double" quotes"

I would like to remove the inner double quotes and keep the outer ones intact. Which function should I be using?

Essentially I should get the below output -

COL_A
-----
"trying to remove double quotes"
2 Answers

There are probably many ways to do this, one quick and dirty solution would be to replace all the double quotes and then just add the first and last quote, something like this:

with cte as (select '\"trying to remove \"double\" quotes\"' as colA)
select colA, '\"'||replace(colA, '\"')||'\"' as colA_replaced
from cte;

depending if you want to handle strings that are not always wrapped in double quotes. doing a replacement on the substring allows for how saving the input how it arrived. If always want one lot of quotes around you string, then Mara's answer works nicely.

select 
    column1
    ,'\"' || replace(column1, '\"') || '\"' as majas_answer
    ,left(column1,1) || replace(substr(column1, 2, length(column1)-2), '"') || right(column1,1) as answer
from values 
    ('"trying to remove "double" quotes"'),
    ('no quotes'),
    ('"only outer quotes"'),
    ('"single inner" quote"');
COLUMN1 MAJAS_ANSWER ANSWER
"trying to remove "double" quotes" "trying to remove double quotes" "trying to remove double quotes"
no quotes "no quotes" no quotes
"only outer quotes" "only outer quotes" "only outer quotes"
"single inner" quote" "single inner quote" "single inner quote"
Related