Substring Extraction & Text Parsing: LEFT, RIGHT, MID, LEN
Overview & Learning Objectives
Raw business records are packed with coded strings like GST numbers, employee IDs, and invoice codes. Functions like LEFT, RIGHT, MID, and LEN allow analysts to slice apart and extract specific substrings effortlessly.
- Extract state codes from Indian GSTINs using LEFT.
- Extract PAN numbers from 15-digit GSTINs using MID.
- Extract checksum characters using RIGHT.
- Combine LEN with text functions for dynamic string extraction.
Industry Standard
Standard: Tax analysts at EY and PwC use text extraction formulas to validate 15-digit Indian GSTINs and separate entity types for reconciliation.
Real-World Example: GSTIN & PAN Invoicing Data Sanitization
At TaxCorp Solutions, an analyst receives 10,000 vendor GSTIN numbers (e.g. `29AAAAA0000A1Z5`). They need to extract the 2-digit State Code (`29` = Karnataka), the 10-digit PAN (`AAAAA0000A`), and the 1-digit entity code (`1`).
| Vendor GSTIN | State Code (LEFT) | PAN Number (MID) | Entity Code (MID) | Checksum (RIGHT) |
|---|---|---|---|---|
| 29ABCDE1234F1Z5 | 29 | ABCDE1234F | 1 | 5 |
| 07PQXYZ9876M2ZA | 07 | PQXYZ9876M | 2 | A |
| 27LMNOA5555K1Z9 | 27 | LMNOA5555K | 1 | 9 |
Functions Covered in this Chapter
Here is the definition, exact syntax, parameter breakdown, and real-world business example for each function covered in this chapter:
LEFT Function
Definition: Extracts a specified number of characters starting from the very beginning (left side) of a text string.
=LEFT(text, [num_chars])
text: The cell containing the text string (e.g. A2).[num_chars]: Number of characters to extract from the left (default is 1).
=LEFT(A2, 2)RIGHT Function
Definition: Extracts a specified number of characters starting from the very end (right side) of a text string.
=RIGHT(text, [num_chars])
text: The cell containing the text string.[num_chars]: Number of characters to extract from the right (default is 1).
=RIGHT(A2, 1)MID Function
Definition: Extracts a specified number of characters from the middle of a text string, starting at any position you choose.
=MID(text, start_num, num_chars)
text: The source text string.start_num: The 1-based character position where extraction begins.num_chars: The number of characters to extract.
=MID(A2, 3, 10)LEN Function
Definition: Calculates the total character count (including spaces and punctuation) of a text string.
=LEN(text)
text: The text string whose length you want to measure.
=IF(LEN(A2)=15, "Valid Length", "Invalid Length")Common Mistakes & Limitations
- Expecting numbers extracted via LEFT/RIGHT/MID to behave as numbers. Text functions ALWAYS return text. Wrap in `VALUE()` or multiply by 1 to convert to numbers.
- Off-by-one errors in MID `start_num`. Remember Excel uses 1-based indexing, not 0-based.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Flash Fill Pattern Recognition | Ctrl + E | Ctrl + E |
| Text to Columns Wizard | Alt + A + E | Data Ribbon |
Pro Tips & Modern Alternatives
- Use `TEXTBEFORE()` and `TEXTAFTER()` in modern Excel 365 to extract text around delimiters without character counting.
- Use `Ctrl + E` (Flash Fill) for quick one-off extractions when formulas are not required.