Handling Backslash Escape Characters

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Handling Backslash Escape Characters

Post by JimKnicely » Thu Mar 07, 2013 4:20 am

In computing and telecommunication, an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence. The typical escape character used is the backslash character.

Vertica uses standard conforming strings as specified in the SQL standard, which means that backslashes are treated as string literals, not escape characters.

But what if I want backslashes to be treated as an escape character? One way is to turn off standard conforming strings!

Example:

The “\n” escape sequence typically is translated into a new line character. But in Vertica, by default it’s treated literally as “\n”.

Code: Select all

dbadmin=> SELECT 'New\nLine';
?column?
-----------
New\nLine
(1 row)
If I want the backslash to be treated as an escape character I could set the STANDARD_CONFORMING_STRINGS variable to OFF. It’s ON by default.

Code: Select all

dbadmin=> SET standard_conforming_strings TO off;
SET
 
dbadmin=> SELECT 'New\nLine';
WARNING 4168:  Nonstandard use of escape in a string literal at or near "'New\n" at character 8
HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'
?column?
----------
New
Line
(1 row)
Notice that a new line appears in the output of the query, but what’s up with that ugly warning? We can get rid of that nonsense by setting the ESCAPE_STRING_WARNING variable to OFF. It’s ON by default.

Code: Select all

dbadmin=> SET escape_string_warning TO off;
SET
 
dbadmin=> SELECT 'New\nLine';
?column?
----------
New
Line
(1 row)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica Tips, Lessons and Examples”