mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Concatenate Strings With A Separator as a Postgres TIL
This commit is contained in:
@@ -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).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1485 TILs and counting..._
|
_1486 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -723,6 +723,7 @@ _1485 TILs and counting..._
|
|||||||
- [Compute Hashes With pgcrypto](postgres/compute-hashes-with-pgcrypto.md)
|
- [Compute Hashes With pgcrypto](postgres/compute-hashes-with-pgcrypto.md)
|
||||||
- [Compute The Levenshtein Distance Of Two Strings](postgres/compute-the-levenshtein-distance-of-two-strings.md)
|
- [Compute The Levenshtein Distance Of Two Strings](postgres/compute-the-levenshtein-distance-of-two-strings.md)
|
||||||
- [Compute The md5 Hash Of A String](postgres/compute-the-md5-hash-of-a-string.md)
|
- [Compute The md5 Hash Of A String](postgres/compute-the-md5-hash-of-a-string.md)
|
||||||
|
- [Concatenate Strings With A Separator](postgres/concatenate-strings-with-a-separator.md)
|
||||||
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
||||||
- [Constructing A Range Of Dates](postgres/constructing-a-range-of-dates.md)
|
- [Constructing A Range Of Dates](postgres/constructing-a-range-of-dates.md)
|
||||||
- [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md)
|
- [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md)
|
||||||
|
|||||||
41
postgres/concatenate-strings-with-a-separator.md
Normal file
41
postgres/concatenate-strings-with-a-separator.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Concatenate Strings With A Separator
|
||||||
|
|
||||||
|
I was putting together an example of using a generated column that concatenates
|
||||||
|
string values from a few other columns. I used manual concatenation with the
|
||||||
|
`||` operator like so:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
create table folders (
|
||||||
|
id integer generated always as identity primary key,
|
||||||
|
user_id integer not null,
|
||||||
|
name text not null,
|
||||||
|
parent_folder_id integer references folders(id),
|
||||||
|
path text generated always as (
|
||||||
|
user_id::text || ':' || lower(name) || ':' || coalesce(parent_folder_id::text, '0')
|
||||||
|
) stored
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
Instead of doing that manual concatenation for the `path` generated column, I
|
||||||
|
can use
|
||||||
|
[`concat_ws`](https://www.postgresql.org/docs/current/functions-string.html).
|
||||||
|
|
||||||
|
```sql
|
||||||
|
create table folders (
|
||||||
|
id integer generated always as identity primary key,
|
||||||
|
user_id integer not null,
|
||||||
|
name text not null,
|
||||||
|
parent_folder_id integer references folders(id),
|
||||||
|
path text generated always as (
|
||||||
|
concat_ws(
|
||||||
|
':',
|
||||||
|
user_id::text,
|
||||||
|
lower(name),
|
||||||
|
coalesce(parent_folder_id::text, '0')
|
||||||
|
)
|
||||||
|
) stored
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
The first argument to `concat_ws` is the separator I want to use. The remaining
|
||||||
|
arguments are the strings that should be concatenated with that separator.
|
||||||
Reference in New Issue
Block a user