1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Escaping String Literals With Dollar Quoting as a postgres til

This commit is contained in:
jbranchaud
2017-09-30 16:59:37 -05:00
parent ede3fe7256
commit 05964b946d
2 changed files with 29 additions and 1 deletions

View File

@@ -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)