Skip to main content
PYSPARK โ€ข LESSON 55

Substring Extraction with substring()

How do we extract the 4-digit year prefix from an ISO date string using 1-based indexing?

Intermediateโฑ 2 Minutesโญ 340 XP
๐Ÿค” THE QUESTION

How do we extract the 4-digit year prefix from an ISO date string using 1-based indexing?

๐Ÿ’ก WHAT IS IT?

substring(strCol, pos, len) extracts a sub-string starting at 1-based index pos for len characters.

๐ŸŽฏ WHAT IS IT USED FOR?

Extracting date components, parsing fixed-length transaction identifiers, and area code slicing.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, substring

df = df.withColumn("year_prefix", substring(col("event_date"), 1, 4))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Substring Extraction with substring().

  • โœ“ Import substring function
  • โœ“ Specify 1-based start index and length
  • โœ“ Extract 4-digit year prefix