MySQL Select Query - Get only first 10 characters of a value
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Extracting the first ten characters of a column is a common SQL requirement for previews, reports, and lightweight search displays. In MySQL, this is straightforward with string functions, but correctness and performance depend on character encoding, null behavior, and filtering strategy. A good solution uses the right function for readability and avoids turning simple display logic into expensive query patterns.
Basic Extraction With LEFT
The most direct approach is LEFT(column, 10).
LEFT returns the first n characters from the string. If the value is shorter than ten, MySQL returns the full value. If the column is NULL, result is NULL.
This is usually the cleanest expression for prefix extraction.
Equivalent Using SUBSTRING
You can do the same with SUBSTRING.
Both forms are valid. Teams often prefer LEFT for this exact use case because the intent is immediately obvious.
Handling Null and Empty Values
If you need a non-null result, wrap with COALESCE.
This is useful for CSV exports or APIs where null handling should be consistent.
Character Set Considerations
MySQL string functions operate on characters, not bytes, when using character data types and proper collations. With UTF-8 text, this usually gives safe visible prefixes. Still, display semantics can be tricky for combined emoji or multi-code-point grapheme clusters, where ten characters by database rules may not equal ten user-perceived symbols.
For user-facing truncation in multilingual UI, it can be better to keep database extraction simple and apply final display truncation in application code where locale-aware rendering is available.
Prefix Filtering Versus Prefix Display
Do not confuse extraction with filtering. If you are searching by prefix, this is better:
Using LEFT(description, 5) = 'micro' can make index usage less efficient in many cases. Prefix extraction in SELECT is fine. Prefix logic in WHERE should be designed for index-friendly matching.
Sorting by Prefix
If reporting requires sort by first ten characters:
This works, but sorting by expression may be expensive on large tables. If this becomes a hotspot, consider a generated column.
Generated Column for Frequent Prefix Use
For heavy workloads, compute and index prefix once.
Then query directly:
This can improve performance for frequent prefix-based reporting and filtering.
Trimming and Normalization Before Extraction
If source data has inconsistent leading spaces, normalize first.
A prefix from untrimmed strings can produce confusing report output.
API and Report Usage Pattern
A common pattern is returning both full value and preview value:
This avoids extra processing in clients while preserving full data for drill-down.
Common Pitfalls
- Using
LEFTin filtering clauses where index-friendly prefix search is needed. - Assuming ten database characters always equals ten user-visible symbols.
- Forgetting null handling and emitting unexpected null preview values.
- Repeating expensive expression sorting on very large datasets without optimization.
- Mixing display truncation and business logic rules in the same SQL expression.
Summary
- Use
LEFT(column, 10)as the default way to get first ten characters. - '
SUBSTRING(column, 1, 10)is equivalent when preferred by team style.' - Handle null and whitespace normalization deliberately.
- Keep prefix display logic separate from index-sensitive filtering strategy.
- For frequent heavy use, consider generated prefix columns with indexes.
Related reading
- MySQL SELECT statement for the length of the field is greater than 1
- MySQL select where column is not empty
- MySQL SELECT WHERE datetime matches day and not necessarily time
- MySQL selecting rows where a column is null
- MySQL selecting yesterday's date
- MySQL Server has gone away when importing large sql file
- MySQL server startup error 'The server quit without updating PID file
- MySQL sharding and partition in distributed system

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.