From 89e0c4004b5e140d07f0e0e52b9728883d8e8523 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 29 May 2022 12:07:42 -0500 Subject: [PATCH] Add Label Dollar-Quoted Strings With A Tag as a Postgres TIL --- README.md | 3 +- .../label-dollar-quoted-strings-with-a-tag.md | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 postgres/label-dollar-quoted-strings-with-a-tag.md diff --git a/README.md b/README.md index 441675b..05f9def 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1208 TILs and counting..._ +_1209 TILs and counting..._ --- @@ -618,6 +618,7 @@ _1208 TILs and counting..._ - [Integers In Postgres](postgres/integers-in-postgres.md) - [Intervals Of Time By Week](postgres/intervals-of-time-by-week.md) - [Is It Null Or Not Null?](postgres/is-it-null-or-not-null.md) +- [Label Dollar-Quoted Strings With A Tag](postgres/label-dollar-quoted-strings-with-a-tag.md) - [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md) - [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md) - [List All Rows In A Table](postgres/list-all-rows-in-a-table.md) diff --git a/postgres/label-dollar-quoted-strings-with-a-tag.md b/postgres/label-dollar-quoted-strings-with-a-tag.md new file mode 100644 index 0000000..b87fe99 --- /dev/null +++ b/postgres/label-dollar-quoted-strings-with-a-tag.md @@ -0,0 +1,38 @@ +# Label Dollar-Quoted Strings With A Tag + +In [Escaping String Literals with Dollar +Quoting](escaping-string-literals-with-dollar-quoting.md), I showed how +PostgreSQL supports escaped string literals so that you don't have to put +backslashes everywhere. This is done by opening and closing the string with +`$$`. + +What if your string literal is going to contain a sequence of two `$` symbols? +Or a better hypothetical, what if you want to convey some information about +what the string represents? + +For either of these, the _tagged_ dollar-quoting is a great fit. + +```sql +> select $JSON${"name": "Sally's Bistro", "price": "$$$"}$JSON$::jsonb; + jsonb +-------------------------------------------- + {"name": "Sally's Bistro", "price": "$$$"} +(1 row) + +> select $JSON${"name": "Sally's Bistro", "price": "$$$"}$JSON$::jsonb->'name' as name; + name +------------------ + "Sally's Bistro" +(1 row) +``` + +The tagged dollar-quoting allows me to write a string that can be cast to +`jsonb` without having to think about which characters need to be escaped. In +the second example, I'm able to interact with it like any `jsonb` entity. + +Here, our tag is `JSON`. It helps convey that the string literal represents +JSON. A tag "follows the same rules as an unquoted identifier, except that it +cannot contain a dollar sign." The tag goes between the dollar signs and is +case-sensitive. + +[source](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING)