SQL QUOTE_IDENT function
Returns a string quoted as a valid SQL identifier, adding double quotes only when required.
- 類別
- Strings
- Returns
string- In this category
- 112 of 172
語法
quote_ident(string)
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | QUOTENAME(name) always adds brackets. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | Not built in | No identifier quoter; wrap in backticks and double any backticks inside. |
| SQLite | Not built in | No identifier quoter; printf('%w') escapes double quotes inside a name. |
| Oracle | Not built in | No direct equivalent; DBMS_ASSERT.ENQUOTE_NAME uppercases by default. |
範例
PostgreSQL
SELECT quote_ident('order details') AS ident; -- "order details"
PostgreSQL
SELECT format('SELECT count(*) FROM %I', 'Order Lines') AS sql;
小知識
- Valid unquoted identifiers come back unchanged: quote_ident('orders') is orders. Spaces, uppercase letters, a leading digit or a reserved word get double quotes: quote_ident('order') is "order".
- PostgreSQL folds unquoted identifiers to lowercase, so quote_ident('Orders') returns "Orders" to preserve the capital.
- Embedded double quotes are doubled, and format('%I.%I', schema, table) applies the same rules to each argument.
Questions about QUOTE_IDENT
What is the difference between quote_ident and quote_literal?
quote_ident is for identifiers (tables, columns) and uses double quotes; quote_literal is for string values and uses single quotes. Swapping them produces invalid or wrong SQL.
When does quote_ident actually add quotes?
When the text is not a valid unquoted identifier: uppercase letters, spaces or punctuation, a leading digit, or a reserved keyword such as order or user.
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: SQL Server, PostgreSQL.
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.