MariaDB QUOTE(): represent strings as SQL literal text
MariaDB QUOTE() encloses a string in single quotes and adds a backslash before internal single quotes, backslashes, ASCII NUL, and Control+Z. It returns text representing a SQL literal, which is useful for inspecting values. To pass real values from an application, use your driver’s prepared statements and parameter binding. MariaDB Docs · MariaDB QUOTE()
Represent a value containing an apostrophe
Run the first SELECT in a MariaDB client without a table. Enclose the input string in single quotes and double the apostrophe in the example name to enter one apostrophe. This also works when ANSI_QUOTES makes double quotes identify SQL identifiers. MariaDB Docs · MariaDB SQL string literals
SELECT QUOTE('ABC') AS plain_text,
QUOTE('O''Brien') AS apostrophe,
QUOTE('"ABC"') AS double_quotes,
QUOTE('') AS empty_text;
plain_text: 'ABC'
apostrophe: 'O\'Brien'
double_quotes: '"ABC"'
empty_text: ''
In the output, ABC acquires surrounding single quotes. The ASCII apostrophe in O'Brien gains a backslash, while internal double quotes stay as they are. An empty string produces two single quotation marks. Doubling an apostrophe applies to the input SQL source; the backslash belongs to the returned text.
Inspect backslashes and control characters as bytes
SELECT QUOTE(CONCAT('a', CHAR(92), 'b')) AS backslash_text,
HEX(QUOTE(CONCAT('a', CHAR(92), 'b'))) AS backslash_hex;
SELECT QUOTE(CONCAT('a', CHAR(0), 'b', CHAR(26))) AS control_text,
HEX(QUOTE(CONCAT('a', CHAR(0), 'b', CHAR(26)))) AS control_hex;
Create the test bytes with CHAR(92), CHAR(0), and CHAR(26), then apply QUOTE(). The first HEX result is 27615C5C6227, where 5C5C shows two consecutive backslashes. The second is 27615C30625C5A27: a backslash and 0 represent NUL, and a backslash and Z represent Control+Z. A client might escape backslashes again for display. Use HEX() to check the actual bytes. MariaDB Docs · MariaDB HEX()
Distinguish QUOTE(NULL) from SQL NULL
SELECT QUOTE(NULL) AS null_literal_text,
QUOTE('NULL') AS quoted_word,
ISNULL(QUOTE(NULL)) AS is_sql_null,
LENGTH(QUOTE(NULL)) AS text_length,
HEX(QUOTE(NULL)) AS text_bytes,
ISNULL(NULL) AS actual_null;
QUOTE(NULL) returns the four letters NULL without surrounding quotes. ISNULL() returns 1 for a real SQL NULL and 0 for QUOTE(NULL). The result has hex 4E554C4C and a length of four characters. Passing the word 'NULL' as a string produces a result that includes surrounding single quotes.
Bind original values to a prepared statement
PREPARE quote_demo FROM 'SELECT ? AS value_text';
SET @value = 'O''Brien';
EXECUTE quote_demo USING @value;
SET @value = NULL;
EXECUTE quote_demo USING @value;
DEALLOCATE PREPARE quote_demo;
Run the next example in one MariaDB connection in ordinary SQL mode. Leave the question mark in SELECT unquoted so it marks a value position. The first EXECUTE returns O'Brien as entered, and the second returns a real SQL NULL. In both cases the value is passed without applying QUOTE(). MariaDB Docs · MariaDB PREPARE MariaDB Docs · MariaDB EXECUTE
In application code, use the driver's prepare and execute API. Bind parameters where values are allowed. For changing table names, column names or keywords, select from allowed SQL templates. For a literal percent sign or underscore in a LIKE search, apply LIKE’s pattern escape rules separately. MariaDB Docs · MariaDB LIKE
Check SQL modes and output formats
Interpretation of QUOTE() output depends on the character set and SQL mode of the connection reading the text. Under NO_BACKSLASH_ESCAPES, a backslash is an ordinary character. When exporting or displaying values, use the encoder appropriate to the format, such as SQL literals, HTML, JSON, or CSV. MariaDB Docs · MariaDB NO_BACKSLASH_ESCAPES




