This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-23. For the latest, interactive version please visit https://mariadb.com/kb/.

String Literals

Strings are sequences of characters and are enclosed with quotes.

The syntax is:

[_charset_name]'string' [COLLATE collation_name]

For example:

'The MariaDB Foundation'
_utf8 'Foundation' COLLATE utf8_unicode_ci;

Strings can either be enclosed in single quotes or in double quotes (the same character must be used to both open and close the string).

The ANSI SQL-standard does not permit double quotes for enclosing strings, and although MariaDB does by default, if the MariaDB server has enabled the ANSI_QUOTES_SQL SQL_MODE, double quotes will be treated as being used for identifiers instead of strings.

Strings that are next to each other are automatically concatenated. For example:

'The ' 'MariaDB ' 'Foundation'

and

'The MariaDB Foundation'

are equivalent.

The \ (backslash character) is used to escape characters (unless the SQL_MODE hasn't been set to NO_BACKSLASH_ESCAPES). For example:

'MariaDB's new features'

is not a valid string because of the single quote in the middle of the string, which is treated as if it closes the string, but is actually meant as part of the string, an apostrophe. The backslash character helps in situations like this:

'MariaDB\'s new features'

is now a valid string, and if displayed, will appear without the backslash.

SELECT 'MariaDB\'s new features';
+------------------------+
| MariaDB's new features |
+------------------------+
| MariaDB's new features |
+------------------------+

Another way to escape the quoting character is repeating it twice:

SELECT 'I''m here', """Double""";
+----------+----------+
| I'm here | "Double" |
+----------+----------+
| I'm here | "Double" |
+----------+----------+

Escape Sequences

There are other escape sequences also. Here is a full list:

Escape sequenceCharacter
\0ASCII NUL (0x00).
\'Single quote (“'”).
\"Double quote (“"”).
\bBackspace.
\nNewline, or linefeed,.
\rCarriage return.
\tTab.
\ZASCII 26 (Control+Z). See note following the table.
\\Backslash (“\”).
\%“%” character. See note following the table.
\_A “_” character. See note following the table.

Escaping the % and _ characters can be necessary when using the LIKE operator, which treats them as special characters.

The ASCII 26 character (\Z) needs to be escaped when included in a batch file which needs to be executed in Windows. The reason is that ASCII 26, in Windows, is the end of file (EOF).

Backslash (\), if not used as an escape character, must always be escaped. When followed by a character that is not in the above table, backslashes will simply be ignored.

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.