Skip to content

Commit

Permalink
Merge pull request #4057 from szarnyasg/star-expression
Browse files Browse the repository at this point in the history
Elaborate on renaming column names
  • Loading branch information
szarnyasg authored Nov 13, 2024
2 parents a901294 + 634b05a commit 475bbfd
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions docs/sql/expressions/star.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ SELECT COLUMNS('(id|numbers?)') FROM numbers;
| 2 | 20 |
| 3 | NULL |

The matches of capture groups can be used to rename columns selected by a regular expression:
### Renaming Columns using a `COLUMNS` expression

The matches of capture groups can be used to rename columns selected by a regular expression.
The capture groups are one-indexed; `\0` is the original column name.

For example, to select the first three letters of colum names, run:

```sql
SELECT COLUMNS('(\w{3}).*') AS '\1' FROM numbers;
Expand All @@ -188,7 +193,12 @@ SELECT COLUMNS('(\w{3}).*') AS '\1' FROM numbers;
| 2 | 20 |
| 3 | NULL |

The capture groups are one-indexed; `\0` is the original column name.
To remove a colon (`:`) character in the middle of a column name, run:

```sql
CREATE TABLE tbl ("Foo:Bar" INTEGER, "Foo:Baz" INTEGER, "Foo:Qux" INTEGER);
SELECT COLUMNS('(\w*):(\w*)') AS '\1\2' FROM tbl;
```

## `COLUMNS` Lambda Function

Expand Down Expand Up @@ -230,7 +240,7 @@ With `*COLUMNS`, the expression expands in its parent expression `coalesce`, res

```sql
SELECT coalesce(*COLUMNS(['a', 'b', 'c'])) AS result
FROM (SELECT NULL a, 42 b, true c);
FROM (SELECT NULL AS a, 42 AS b, true AS c);
```

| result |
Expand All @@ -241,7 +251,7 @@ FROM (SELECT NULL a, 42 b, true c);

```sql
SELECT coalesce(*COLUMNS(*)) AS result
FROM (SELECT NULL a, 42 b, true c);
FROM (SELECT NULL a, 42 AS b, true AS c);
```

| result |
Expand Down

0 comments on commit 475bbfd

Please sign in to comment.