How to escape single quotes in Unload
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When an UNLOAD command takes a SQL query as a string literal, you have two quoting layers to think about. The outer layer is the string passed to UNLOAD, and the inner layer is the SQL inside that string. To include a single quote inside the inner SQL, you usually escape it by doubling it again.
The Real Problem Is Nested Quoting
In systems such as Amazon Redshift, UNLOAD often looks like this:
The query is already inside a quoted string. If the query itself needs a string literal containing an apostrophe, you must escape it for SQL inside SQL.
Double Quotes Are Not the Fix for String Literals
A common mistake is trying to use double quotes for string values.
In SQL, string literals use single quotes. Double quotes are generally for identifiers, not string content.
Escape a Single Quote by Doubling It
Inside normal SQL, the string O'Brien is written as O''Brien.
Inside UNLOAD, that inner SQL is itself inside a quoted string, so you still need the doubled single quote in the inner query.
This looks noisy, but it follows a consistent rule:
- the SQL literal
O'BrienbecomesO''Brien - because that SQL is itself inside a string literal, the quote characters in the query text must survive that outer layer too
In practice, every single quote that belongs inside the query text must be doubled appropriately.
A Simpler Way to Think About It
Build the inner query first as if it were a normal standalone SQL statement.
Then wrap that whole query as the UNLOAD string argument, preserving the inner escaping.
This mental split helps because it stops you from trying to solve both quoting layers at once.
QUOTE and ESCAPE Options Are About Output Format
Another source of confusion is the QUOTE or ESCAPE options on the UNLOAD command. Those control how exported file values are written, not how the SQL query string itself is parsed.
For example, this affects CSV output formatting:
That does not change the rule for escaping apostrophes inside the query text. Query parsing and file formatting are separate concerns.
Parameterization Is Better When Available
If the SQL is being generated in application code, manually building nested SQL strings can become fragile. When the surrounding system allows parameterization or safe query composition, use it.
If parameterization is not available for UNLOAD, construct the query carefully in code and treat every embedded literal as a quoting boundary that must be escaped for the target engine.
Test With a Minimal Query First
When debugging quote issues, reduce the statement to the smallest failing example.
If that works, the quote logic is correct and the remaining issue is elsewhere in the full query.
Common Pitfalls
- Confusing SQL string-literal escaping with CSV output escaping.
- Using double quotes for string values instead of single quotes.
- Trying to reason about the full nested statement without first writing the inner query correctly.
- Manually concatenating large SQL strings in application code without a clear escape strategy.
- Forgetting that
UNLOADadds an extra quoting layer around the query text.
Summary
- '
UNLOADoften requires nested SQL string quoting.' - In SQL, embedded apostrophes are escaped by doubling single quotes.
- Build the inner SQL correctly first, then wrap it for
UNLOAD. - Output options such as
QUOTEaffect exported data formatting, not query-literal parsing. - When debugging, reduce the statement to a minimal query that proves the escaping is correct.

