SQL QUOTE_NULLABLE function
Quotes a value as an SQL string literal, and returns the unquoted word NULL when the input is NULL.
- カテゴリ
- Strings
- Returns
string- In this category
- 113 of 172
構文
quote_nullable(string)
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | Not built in | No equivalent; QUOTENAME returns NULL for NULL input. |
| PostgreSQL | はい | 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. |
例
PostgreSQL
SELECT quote_nullable(NULL) AS literal; -- NULL
PostgreSQL
SELECT format('UPDATE t SET parent = %L', NULL::text) AS sql;
知っておくと便利なこと
- 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.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite.
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.