Escaping String Literals

The docs (https://www.cockroachlabs.com/docs/stable/sql-constants.html#string-literals) say that “SQL string literals are formed by an arbitrary sequence of characters enclosed between single quotes”. I want to be sure I understand this correctly, since this implies that CockroachDB’s behavior with string literals differs from that of MySQL’s. Is it correct that the only character that needs to be escaped in a string is the single quote? Not even null bytes or newline characters?

this implies that CockroachDB’s behavior with string literals differs from that of MySQL’s

Right, CockroachDB’s syntax is the same as PostgreSQL’s here (and in most other areas).

Is it correct that the only character that needs to be escaped in a string is the single quote? Not even null bytes or newline characters?

Newlines are fine in a single-quoted string. The NULL character is tricky. PostgreSQL doesn’t appear to allow it in VARCHAR values at all. CockroachDB allows it if you use the E'\x00' syntax for it, but the NULL character cannot appear directly in a string literal (or at least I’m unable to pass such a character through the cockroach sql shell).

1 Like