Use SUBSTRING for a Fixed-Width Batch Prefix
Return record_id, raw_units, and its first two characters as batch_prefix.
- String functions
- Sorting
Exercise brief
Understand the request
Ingestion operations analyst A legacy feed embeds a two-character batch prefix in a zero-padded text field.
A legacy feed embeds a two-character batch prefix in a zero-padded text field. Return record_id, raw_units, and its first two characters as batch_prefix.
Return
- Preserve leading zeroes in batch_prefix.
- Order by record_id ascending.
Constraints
- Use a substring function with a one-based start position.
- Do not cast raw_units before extracting the prefix.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
function_cases
record_idINTEGERfirst_nameTEXTlast_nameTEXTlabelTEXTemailTEXTbackup_emailTEXTphoneTEXTraw_unitsTEXTraw_quantityTEXTraw_statusTEXTactual_valueREALtarget_valueREALcompleted_unitsREALelapsed_hoursREAL
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
SQL substring positions begin at 1 in the supported variants.
Hint 2
The source is text, so leading zeroes remain available.
Hint 3
Extract two characters starting at position 1.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT record_id, raw_units, SUBSTR(raw_units, 1, 2) AS batch_prefix FROM function_cases ORDER BY record_id;Why this works
Substring extraction is safer before numeric conversion when fixed-width text carries meaningful leading zeroes.
Success check
Every prefix is exactly the first two source characters, including leading zeroes.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| record_id | raw_units | batch_prefix |
|---|---|---|
| 101 | 007 | 00 |
| 102 | 012 | 01 |
| 103 | 003 | 00 |
| 104 | 042 | 04 |
| 105 | 000 | 00 |
| 106 | 009 | 00 |
| 107 | 015 | 01 |
| 108 | 100 | 10 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
SELECT Statements
Select columns, filter rows, remove duplicates, and order query results.
CASE Statements & Conditional Logic
Build NULL-aware classifications, precedence-safe decisions, flags, scores, and guarded calculations with portable CASE expressions.
Date Operations & Time-Based Analytics
Practice date arithmetic, safe timestamp ranges, calendar bucketing, dense time series, rolling windows, growth, and cohort analysis.
Open the interactive workspace and practice across SQL topics.