Advanced Excel for Data AnalystsLesson 15 of 49
Lesson 15 of 49

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 GSTINState Code (LEFT)PAN Number (MID)Entity Code (MID)Checksum (RIGHT)
29ABCDE1234F1Z529ABCDE1234F15
07PQXYZ9876M2ZA07PQXYZ9876M2A
27LMNOA5555K1Z927LMNOA5555K19

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:

Function 1

LEFT Function

Definition: Extracts a specified number of characters starting from the very beginning (left side) of a text string.

Syntax:
=LEFT(text, [num_chars])
Parameters Breakdown:
  • text: The cell containing the text string (e.g. A2).
  • [num_chars]: Number of characters to extract from the left (default is 1).
Real-World Example (GST State Code Extraction):
Problem / Scenario: Extract the 2-digit State Code from GSTIN "29ABCDE1234F1Z5" in cell A2.
Formula: =LEFT(A2, 2)
Result: "29" (Karnataka state code)
Why it works: Takes the first 2 characters from the left.
Function 2

RIGHT Function

Definition: Extracts a specified number of characters starting from the very end (right side) of a text string.

Syntax:
=RIGHT(text, [num_chars])
Parameters Breakdown:
  • text: The cell containing the text string.
  • [num_chars]: Number of characters to extract from the right (default is 1).
Real-World Example (Checksum Character Extraction):
Problem / Scenario: Extract the single final checksum character from GSTIN in cell A2.
Formula: =RIGHT(A2, 1)
Result: "5"
Why it works: Takes the 1 rightmost character.
Function 3

MID Function

Definition: Extracts a specified number of characters from the middle of a text string, starting at any position you choose.

Syntax:
=MID(text, start_num, num_chars)
Parameters Breakdown:
  • text: The source text string.
  • start_num: The 1-based character position where extraction begins.
  • num_chars: The number of characters to extract.
Real-World Example (10-Digit PAN Extraction):
Problem / Scenario: Extract the 10-digit PAN number starting at position 3 from "29ABCDE1234F1Z5".
Formula: =MID(A2, 3, 10)
Result: "ABCDE1234F"
Why it works: Starts at character 3 and extracts 10 sequential characters.
Function 4

LEN Function

Definition: Calculates the total character count (including spaces and punctuation) of a text string.

Syntax:
=LEN(text)
Parameters Breakdown:
  • text: The text string whose length you want to measure.
Real-World Example (GSTIN Format Length Validation):
Problem / Scenario: Verify if the GSTIN in cell A2 has the mandatory 15 characters.
Formula: =IF(LEN(A2)=15, "Valid Length", "Invalid Length")
Result: "Valid Length"
Why it works: Counts total characters; if not 15, flags data error.

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

ActionWindowsMac
Flash Fill Pattern RecognitionCtrl + ECtrl + E
Text to Columns WizardAlt + A + EData 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.