'snowflake regex_replace not working as expected
I am trying to do this that strips "['" or "']" in the string. For Example, if we have ['Customer Name'] it should be "Customer Name"
select regexp_replace("['Customers NY']","\\['|\\']","") as customername;
I am getting this error-- SQL compilation error: error line 1 at position 22 invalid identifier "['Customers NY']"
Solution 1:[1]
It's a typo..
ALL string in SQL are single quotes only.. you double quotes are for named objects like columns/tables.
Then you will have to escape the quotes in the quotes
select regexp_replace('[\'Customers NY\']','\\[\'|\'\\]','') as customername;
gives:
| CUSTOMERNAME |
|---|
| Customers NY |
Solution 2:[2]
Double $$ makes escaping easier. Combine that with translate and you could do
select translate('[\'Customers NY\']',$$[']$$,'');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simeon Pilgrim |
| Solution 2 | Phil Coulson |
