PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, dollar-quoted string constants, also known as dollar-quoting, provide a way to include string literals and escape sequences in SQL scripts without the need to escape single quotes or backslashes. Dollar-quoting is particularly useful when writing functions or complex SQL statements, as it avoids common escaping issues.
A dollar-quoted string constant is specified with $$
or between two dollar signs with an optional "tag" in between. The format is:
$tag$your string content here$tag$
SELECT $$Hello, World!$$;
SELECT $example$Hello, World!$example$;
Without dollar-quoting:
CREATE FUNCTION example() RETURNS text AS ' BEGIN RETURN ''Hello, World!''; END; ' LANGUAGE plpgsql;
With dollar-quoting:
CREATE FUNCTION example() RETURNS text AS $$ BEGIN RETURN 'Hello, World!'; END; $$ LANGUAGE plpgsql;
Using dollar-quoting makes it clearer and eliminates the need for doubling up single quotes.
If your string content has single quotes or backslashes, dollar-quoting simplifies things:
SELECT $$It's a "special" day.\nLet's celebrate!$$;
This returns:
It's a "special" day.\nLet's celebrate!
Readability: Dollar-quoting can improve the readability of SQL scripts, especially when creating or modifying stored procedures and functions.
Simplifies escaping: Eliminates the need for multiple escape sequences in long strings or when writing procedural code.
Flexibility with tags: You can use custom tags to further distinguish different sections of dollar-quoted strings, making the code clearer.
Remember that dollar-quoting is a feature specific to PostgreSQL, so it might not be available or might work differently in other database systems. When using dollar-quoting, it's important to ensure the chosen delimiter (the tag) does not appear within the string content itself to avoid unexpected issues.
Using dollar-quoted strings for escaping characters in PostgreSQL:
SELECT $$This is a dollar-quoted string with 'single quotes'$$ AS result;
Multiline string constants in PostgreSQL with dollar quotes:
SELECT $$ This is a multiline string $$ AS result;
Benefits and use cases of dollar-quoted string constants in PostgreSQL:
SELECT $$ Benefits and use cases of dollar-quoted strings $$ AS result;
Using different delimiter symbols with dollar-quoted strings in PostgreSQL:
SELECT $custom$ Using a custom delimiter with dollar-quoted strings $custom$ AS result;
Combining dollar-quoted strings with string functions in PostgreSQL:
SELECT $$ Concatenating strings with dollar quotes $$ || ' in PostgreSQL' AS result;
Dollar-quoted strings vs standard single-quoted strings in PostgreSQL:
SELECT 'Standard single-quoted string' AS standard, $$Dollar-quoted string$$ AS dollar_quoted;