// strings reference

SQL QUOTE function

Returns the argument as a quoted SQL string literal, escaping embedded quotes (and, on MySQL, backslashes). Useful for generating SQL text; parameters are better for running it.

Catégorie
Strings
Returns
string
In this category
59 of 172

Syntaxe

QUOTE(string)

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Different name or syntax QUOTENAME(s, '''') for strings up to 128 characters; longer input returns NULL
PostgreSQL Different name or syntax quote_literal(s), or quote_nullable(s) to get the bare word NULL
MySQL Oui escapes ', \ and NUL; result is wrapped in single quotes
SQLite Oui doubles embedded quotes; numbers come back bare, blobs as X'..' literals
Oracle Not built in no equivalent; double the quotes with REPLACE(s, '''', '''''')

Exemples

MySQL

SELECT QUOTE('O''Brien') AS lit;  -- 'O\'Brien'

MySQL

SELECT QUOTE(NULL) AS lit;  -- NULL (unquoted)

SQLite

SELECT quote('O''Brien') AS lit, quote(x'ab') AS blob_lit;  -- 'O''Brien', X'AB'

Bon à savoir

  • The result includes the surrounding quotes: QUOTE('ab') returns 'ab' with quotes included, so it slots straight into a composed statement.
  • MySQL escapes with a backslash: QUOTE('O''Brien') is 'O\'Brien', which only parses where backslash escapes are on. SQLite doubles the quote instead and returns 'O''Brien', which parses everywhere.
  • It is for string literals only, not identifiers. MySQL's identifier helper is sys.quote_identifier() in the sys schema.
  • Parameterized queries beat QUOTE every time: no escaping logic, no injection surface. QUOTE is for the rare dynamic case you cannot parameterize.

Questions about QUOTE

Is QUOTE safe against SQL injection?

For string literals on MySQL and SQLite, it escapes the dangerous characters correctly, as long as the SQL runs on the same engine with the same escape settings. It does not cover identifiers, numbers from strings, or other engines. Parameters remain the right answer.

Why does the output include the quotes?

QUOTE returns a ready-to-paste literal: quotes and escapes included. QUOTE('ab') yields 'ab', four characters counting the quotes.

What is the SQL Server equivalent?

QUOTENAME, which quotes identifiers with brackets by default. QUOTENAME(s, '''') returns a quoted string literal with embedded quotes doubled, but only for input up to 128 characters; past that it returns NULL, so longer values need '''' + REPLACE(s, '''', '''''') + ''''.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.