QUOTE
Description
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.
Syntax
QUOTE(<str>)
Parameters
| Parameter | Description |
|---|---|
<str> | The input string to be quoted. Type: VARCHAR |
Return Value
Returns VARCHAR type, the string wrapped with single quotes and with special characters escaped.
Special cases:
- If input is NULL, returns the string 'NULL' (without quotes)
- Single quotes
'are escaped to\' - Backslashes
\are escaped to\\ \\is escaped to\- Empty string returns
''
Examples
- Basic string quoting
SELECT quote('hello');
+----------------+
| quote('hello') |
+----------------+
| 'hello' |
+----------------+
- String with single quotes (will be escaped)
SELECT quote("It's a test");
+----------------------+
| quote("It's a test") |
+----------------------+
| 'It's a test' |
+----------------------+
- NULL value handling
SELECT quote(NULL);
+-------------+
| quote(NULL) |
+-------------+
| NULL |
+-------------+
- Empty string handling
SELECT quote('');
+-----------+
| quote('') |
+-----------+
| '' |
+-----------+
- Backslash character
SELECT quote('aaa\\');
+----------------+
| quote('aaa\\') |
+----------------+
| 'aaa\' |
+----------------+
SELECT quote('aaa\cccb');
+-------------------+
| quote('aaa\cccb') |
+-------------------+
| 'aaacccb' |
+-------------------+