diff --git a/README.md b/README.md index 779c807..ca796f9 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_567 TILs and counting..._ +_568 TILs and counting..._ --- @@ -296,6 +296,7 @@ _567 TILs and counting..._ - [Dump And Restore A Database](postgres/dump-and-restore-a-database.md) - [Edit Existing Functions](postgres/edit-existing-functions.md) - [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md) +- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md) - [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md) - [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md) - [Find The Data Directory](postgres/find-the-data-directory.md) diff --git a/postgres/escaping-string-literals-with-dollar-quoting.md b/postgres/escaping-string-literals-with-dollar-quoting.md new file mode 100644 index 0000000..1c2eaf0 --- /dev/null +++ b/postgres/escaping-string-literals-with-dollar-quoting.md @@ -0,0 +1,27 @@ +# Escaping String Literals With Dollar Quoting + +String literals in PostgreSQL are defined by surrounding the content with +the `'` character. For string literals that contain the `'` character, you +may have seen it escaped with a preceding `'`. + +```sql +> select 'Isn''t this nice?'; + ?column? +------------------ + Isn't this nice? +``` + +This is easy enough to do, but can be error prone and doesn't work well if +SQL is being programmatically generated. A great workaround is to escape +string literals using what is called dollar quoting. + +```sql +> select $$Isn't this even nicer?$$; + ?column? +------------------------ + Isn't this even nicer? +``` + +Just wrap both ends in `$$` instead of `'`. + +[source](https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html)