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:

  1. Selects sample_id and description from {{ ref("rock_samples") }}
  2. Extracts the numeric portion from description using a regular expression and aliases it as age
  3. Returns an empty string '' for age when 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 age column is returned as a string. For S3 (Limestone), there is no numeric part so age is an empty string '', not NULL.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →