The QUOTE function is used to wrap a string with single quotes and escape special characters within it, making it safe for use in SQL statements.
QUOTE(<str>)
| Parameter | Description |
|---|---|
<str> | The input string to be quoted. Type: VARCHAR |
Returns VARCHAR type, the string wrapped with single quotes and with special characters escaped.
Special cases:
' are escaped to \'\ are escaped to \\''SELECT quote('hello');
+----------------+ | quote('hello') | +----------------+ | 'hello' | +----------------+
SELECT quote("It's a test");
+----------------------+ | quote("It's a test") | +----------------------+ | 'It\'s a test' | +----------------------+
SELECT quote(NULL);
+-------------+ | quote(NULL) | +-------------+ | NULL | +-------------+
SELECT quote('');
+-----------+ | quote('') | +-----------+ | '' | +-----------+
SELECT quote('aaa\\');
+----------------+ | quote('aaa\\') | +----------------+ | 'aaa\' | +----------------+
SELECT quote('aaa\cccb');
+-------------------+ | quote('aaa\cccb') | +-------------------+ | 'aaacccb' | +-------------------+