Skip to main content

String literals

A string literal is a sequence of characters enclosed in single quotes ' or double quotes ".

info

If ANSI_QUOTES is enabled, only single quotes ' can be used to enclose string literals. The following statements set and query this mode:

SET sql_mode = 'ANSI_QUOTES';
SELECT @@sql_mode;

String literals can be binary or non-binary:

  • Binary string literals. These are measured in bytes and compared byte by byte. The character set and collation are binary.

  • Non-binary string literals. These are measured in characters and compared character by character. They support a variety of character sets and collations other than binary.

Use the COLLATE clause to specify the character set and collation for a string literal:

[_charset_name]'string' [COLLATE collation_name]

In a string, an escape character is a sequence of characters that starts with a backslash \ and has a special meaning. Escape characters are case-sensitive. For example, \b represents a backspace, while \B represents the character B.

The following table lists all escape characters:

Escape characterMeaning
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tTab
\\Backslash
\'Single quote
\"Double quote
\_Underscore
\%Percent sign
\0NULL
\ZASCII 26 control character Ctrl+Z