Numeric Age from Rock Sample
Beginner Mode
Scenario
A geologist is working with a dataset of rock samples where each description contains a mix of letters and numbers representing the rock's age in millions of years.
Task
Write a Snowflake SQL query that:
- Selects
sample_idanddescriptionfrom{{ ref("rock_samples") }} - Extracts the numeric portion from
descriptionusing a regular expression and aliases it asage - Returns an empty string
''foragewhen no numeric value is present in the description
Schema
rock_samples
| Column | Type | Description |
|---|---|---|
| sample_id | String | Unique sample identifier |
| description | String | Rock type with optional age suffix (e.g., "Basalt_450Ma") |
Example
rock_samples:
| sample_id | description |
|---|---|
| S1 | Basalt_450Ma |
| S2 | Sandstone_300Ma |
| S3 | Limestone |
| S4 | Granite_200Ma |
| S5 | Marble_1800Ma |
Expected Output:
| sample_id | description | age |
|---|---|---|
| S1 | Basalt_450Ma | 450 |
| S2 | Sandstone_300Ma | 300 |
| S3 | Limestone | |
| S4 | Granite_200Ma | 200 |
| S5 | Marble_1800Ma | 1800 |
Note: The
agecolumn is returned as a string. For S3 (Limestone), there is no numeric part soageis an empty string'', notNULL.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Amazon