Mastering MySQL String Functions: A Comprehensive Guide

 3 min read

YouTube video ID: KRXSJb9ql1Y

Source: YouTube video by Alex The AnalystWatch original video

PDF

Introduction

MySQL provides a rich set of built‑in string functions that simplify working with textual data. This article walks through the most commonly used functions, illustrates their syntax, and shares practical use‑cases drawn from real‑world data‑cleaning tasks.

LENGTH – Measuring String Size

  • Syntax: SELECT LENGTH('your_string');
  • Returns the number of characters in the given string.
  • Example: SELECT LENGTH('Skyfall');7.
  • Practical use‑case: Verify that phone numbers contain exactly 10 characters; flag any rows where LENGTH(phone_number) <> 10 for cleaning.

UPPER and LOWER – Standardizing Case

  • UPPER converts all characters to uppercase: SELECT UPPER(first_name) FROM employees;
  • LOWER converts all characters to lowercase: SELECT LOWER(first_name) FROM employees;
  • Why it matters: Data entry may be inconsistent (e.g., "Tom" vs "tom"). Converting to a single case helps enforce uniformity before further processing or comparison.

TRIM, LTRIM, RTRIM – Removing Unwanted Spaces

  • TRIM(string) removes leading and trailing whitespace.
  • LTRIM(string) removes only leading spaces.
  • RTRIM(string) removes only trailing spaces.
  • Example: SELECT TRIM(' sky ');'sky'.
  • Use‑case: Clean up user‑entered text fields where extra spaces cause mismatches in joins or filters.

LEFT and RIGHT – Extracting Fixed‑Length Segments

  • LEFT(string, n) returns the leftmost n characters.
  • RIGHT(string, n) returns the rightmost n characters.
  • Example: SELECT LEFT(first_name, 4) FROM employees; yields the first four letters of each name.
  • When to use: Quickly obtain prefixes or suffixes, such as extracting area codes from phone numbers.

SUBSTRING – Flexible Sub‑String Extraction

  • Syntax: SUBSTRING(string, start_position, length)
  • start_position is 1‑based; length is optional (returns the rest of the string if omitted).
  • Example: SELECT SUBSTRING(first_name, 3, 2) FROM employees; → returns characters at positions 3‑4 (e.g., "Leslie" → "SL").
  • Real‑world scenario: Pull the month from a YYYY-MM-DD date stored as a string: SUBSTRING(birth_date, 6, 2) yields the month component.

REPLACE – Substituting Characters or Sub‑Strings

  • Syntax: REPLACE(string, from_substring, to_substring)
  • Replaces every occurrence of from_substring with to_substring.
  • Example: SELECT REPLACE(first_name, 'a', 'z') FROM employees; changes all lower‑case "a" to "z".
  • Use‑case: Correct systematic typos or mask sensitive characters.

LOCATE – Finding the Position of a Sub‑String

  • Syntax: LOCATE(substring, string)
  • Returns the 1‑based index of the first occurrence of substring within string, or 0 if not found.
  • Example: SELECT LOCATE('x', 'Alexander');4.
  • Application: Identify rows containing a specific pattern (e.g., names containing "an") and filter based on the returned position.

CONCAT – Merging Multiple Columns

  • Syntax: CONCAT(str1, str2, ..., strN)
  • Concatenates the supplied strings into a single result.
  • Example: SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
  • Why it’s essential: Frequently needed to build full names, addresses, or composite keys from separate columns.

Putting It All Together

By combining these functions, you can perform sophisticated text transformations in a single query. For instance, to create a clean, title‑cased full name and extract the birth month, you might write:

SELECT
  CONCAT(UPPER(LEFT(first_name,1)), LOWER(SUBSTRING(first_name,2))) AS clean_first,
  CONCAT(UPPER(LEFT(last_name,1)), LOWER(SUBSTRING(last_name,2))) AS clean_last,
  CONCAT(clean_first, ' ', clean_last) AS full_name,
  SUBSTRING(birth_date,6,2) AS birth_month
FROM employees;

This demonstrates how length checks, case conversion, trimming, substring extraction, and concatenation can be chained to produce polished results.

Additional Resources

The full course also covers numeric functions, date‑time functions, and data‑type conversions, all part of the broader MySQL Functions module.

String functions are indispensable tools for cleaning, standardizing, and reshaping textual data in MySQL. Mastering them—especially LENGTH, UPPER/LOWER, TRIM, LEFT/RIGHT, SUBSTRING, REPLACE, LOCATE, and CONCAT—enables you to write concise queries that replace manual data‑wrangling, improve data quality, and streamline downstream analysis.

Frequently Asked Questions

Who is Alex The Analyst on YouTube?

Alex The Analyst is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.

Does this page include the full transcript of the video?

Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.

PDF