Replace Function with Backslash in Snowflake

Viewed 324

Why does the query

select replace('its a\b\c\d','\\\\','\\\\\\\\')  

give the result 'its acd' while executed in Snowflake

where as the other SQL compiler produces the result 'its a\b\c\d'.

1 Answers

Escape Sequences:

Within a single-quoted string constant, a single quote character, along with other special characters, can alternatively be included in escape sequences. A backslash character (\) begins a backslash escape sequence.

Note that a non-special character escaped with a backslash is simply interpreted as that character (e.g. '\z' becomes 'z').

The string:

SELECT 'its a\b\c\d', '\\', '\\\\'
=>
its a<BS>cd, '\', '\\'

SELECT '\a', '\c', '\d'
=>
a, c, d

The symbol \b is the backspace character.

SELECT ASCII('\b');
=> 8

The REPLACE is trying to replace \(which does not existing in the input string) with \\ and it is a reason why it returns: its a<BS>cd


To perform the replacement the dollar-quoting could be used:

SELECT $$its a\b\c\d$$, $$\$$, $$\\$$;
=>
its a\\b\\c\\d
Related