Calculated field
REPLACE([label], 'birch', 'oak')
Sample data
| label | quantity | amount | flag |
|---|---|---|---|
| cedar cedar | 65 | 12.8 | True |
| ORCHID-west | 66 | -7.6 | False |
| maple-east | 67 | 0 | null |
| willow north | 68 | 25.3 | True |
| birch-birch | 69 | 4.9 | False |
| pine/south | 70 | -1.2 | null |
| ash | 71 | 9 | True |
| null | null | null | null |
The same calculation as SQL
SELECT replace(label,'birch','oak') AS result FROM input
Runs the example above.
SELECT replace(label,'birch','oak') AS result FROM input
Written for this dialect; not run here.
SELECT replace(label,'birch','oak') AS result FROM input
Written for this dialect; not run here.
Result
| result |
|---|
| cedar cedar |
| ORCHID-west |
| maple-east |
| willow north |
| oak-oak |
| pine/south |
| ash |
| null |
Same rows from the calculation and from the SQL in DuckDB.
Where a naive translation goes wrong
- DuckDB regexp_replace without g changes only the first match.
Naive SQL
SELECT regexp_replace(label,'birch','oak') AS result FROM input
result cedar cedar ORCHID-west maple-east willow north oak-birch pine/south ash null Matches the calculation
SELECT replace(label,'birch','oak') AS result FROM input
result cedar cedar ORCHID-west maple-east willow north oak-oak pine/south ash null
Signature
REPLACE(string, string, expression)