Skip to main content

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

ParameterDescription
<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

  1. Basic string quoting
SELECT quote('hello');
+----------------+
| quote('hello') |
+----------------+
| 'hello' |
+----------------+
  1. String with single quotes (will be escaped)
SELECT quote("It's a test");
+----------------------+
| quote("It's a test") |
+----------------------+
| 'It's a test' |
+----------------------+
  1. NULL value handling
SELECT quote(NULL);
+-------------+
| quote(NULL) |
+-------------+
| NULL |
+-------------+
  1. Empty string handling
SELECT quote('');
+-----------+
| quote('') |
+-----------+
| '' |
+-----------+
  1. Backslash character
SELECT quote('aaa\\');
+----------------+
| quote('aaa\\') |
+----------------+
| 'aaa\' |
+----------------+
SELECT quote('aaa\cccb');
+-------------------+
| quote('aaa\cccb') |
+-------------------+
| 'aaacccb' |
+-------------------+