how to trim leading zeros from alphanumeric text in mysql function
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If the goal is to remove zeros only from the very start of a string, the rule is simple: delete the longest prefix that matches one or more 0 characters, then keep the rest untouched. That works for purely numeric values like 000123 and for alphanumeric values like 000ABC123.
In modern MySQL, the cleanest solution is a regular expression anchored at the beginning of the string. Older versions can still solve it, but the SQL is less elegant.
The Simplest Solution in MySQL 8
MySQL 8 provides REGEXP_REPLACE, which makes this task straightforward.
Results:
- '
000ABC123becomesABC123' - '
000123becomes123' - '
ABC001staysABC001'
The pattern ^0+ means “one or more zeros at the start of the string.” Because the expression is anchored with ^, zeros in the middle are left alone.
If you need to apply this to a column, the update is equally direct:
The WHERE clause avoids touching rows that already do not start with zero.
Turning It Into a Reusable Function
If the same transformation appears in many queries, a stored function can keep the SQL readable.
Usage:
That returns X45.
If your business rule says that an all-zero string such as 0000 should become 0 rather than the empty string, add a wrapper.
Older MySQL Versions
If REGEXP_REPLACE is unavailable, you can still find the first non-zero character and slice from there. One approach is to use REGEXP_INSTR where available, or a more manual expression if necessary.
For purely numeric strings, developers sometimes cast to UNSIGNED, but that is not appropriate for general alphanumeric text because values like 000ABC123 do not cast meaningfully.
That distinction matters. This is a string-manipulation problem, not a numeric-conversion problem.
Data Semantics Matter
Before trimming, confirm that leading zeros are actually formatting noise. In many systems they are significant. Product codes, ZIP codes, bank identifiers, and fixed-width protocol fields often require those zeros to remain.
If the column mixes true identifiers with display values, consider creating a normalized companion column rather than overwriting the stored original. That gives you search-friendly data without losing the canonical representation.
Common Pitfalls
A common mistake is using TRIM(LEADING '0' FROM col) without checking behavior across versions and business rules. It can work for simple cases, but teams often forget to define what should happen with values such as 0000, 0A01, or empty strings.
Another mistake is casting to a number to remove zeros. That destroys non-numeric suffixes and can silently change the meaning of the data.
Developers also sometimes remove all zeros, not just leading zeros, by using an unanchored replacement. Always anchor the pattern to the beginning when the rule is “leading only.”
Finally, be careful with updates on identifier columns. If external systems still expect the padded form, trimming in place can break joins, lookups, or audit trails.
Summary
- For MySQL 8, use
REGEXP_REPLACE(col, '^0+', ''). - The pattern removes only the zeros at the start of the string.
- Do not cast alphanumeric data to numeric types just to strip zeros.
- Decide explicitly how to handle all-zero strings such as
0000. - Verify that leading zeros are not semantically important before updating stored values.
Related reading
- How to truncate a foreign key constrained table?
- How to truncate a foreign key constrained table?
- How to understand when shedlock was acquired and released?
- How to unload a table on RedShift to a single CSV file?
- How to update a record using sequelize for node?
- How to update an item in Dynamodb of type String Set SS?
- How to update an item in Dynamodb of type String Set SS?
- How to update column with null value

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.