// strings reference

SQL QUOTE_NULLABLE function

Quotes a value as an SQL string literal, and returns the unquoted word NULL when the input is NULL.

Kategorie
Strings
Returns
string
In this category
113 of 172

Syntax

quote_nullable(string)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Not built in No equivalent; QUOTENAME returns NULL for NULL input.
PostgreSQL Ja Supported under this name.
MySQL Different name or syntax QUOTE(NULL) also returns the word NULL.
SQLite Different name or syntax quote(NULL) also returns the word NULL.
Oracle Not built in No equivalent; DBMS_ASSERT.ENQUOTE_LITERAL plus NVL.

Beispiele

PostgreSQL

SELECT quote_nullable(NULL) AS literal;  -- NULL

PostgreSQL

SELECT format('UPDATE t SET parent = %L', NULL::text) AS sql;

Gut zu wissen

  • quote_literal(NULL) is NULL, so it wipes out a concatenated query string; quote_nullable(NULL) is the text NULL and survives concatenation.
  • format()'s %L specifier behaves like quote_nullable, which is why format() is the usual way to build dynamic SQL in PL/pgSQL.
  • It protects values only; identifiers need quote_ident or format's %I.

Questions about QUOTE_NULLABLE

Why choose quote_nullable over quote_literal?

When the value can be NULL and the generated SQL must still be valid: the literal becomes the keyword NULL instead of nulling the whole string.

Does quote_nullable change non-null values?

No. It doubles embedded quotes and wraps the text exactly as quote_literal does; only the NULL case differs.

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: 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.