Mastering MySQL for Data AnalystsLesson 37 of 42
Lesson 37 of 42

Type Casting: CAST() & CONVERT() Functions in MySQL

Overview & Learning Objectives

In production analytics pipelines, data types frequently arrive mismatched: transaction amounts stored as `VARCHAR` text strings, or float numbers needing conversion to fixed decimals. MySQL provides explicit type conversion via `CAST()` and `CONVERT()`. Understanding explicit casting prevents dangerous implicit coercion bugs where MySQL silently coerces strings to numbers during joins and invalidates indexes.

  • Cast data explicitly using ANSI standard `CAST(expression AS target_type)`.
  • Understand MySQL cast target types: `SIGNED`, `UNSIGNED`, `DECIMAL`, `CHAR`, `DATE`, `DATETIME`.
  • Diagnose and prevent implicit type coercion performance traps in query filters.

Industry Standard

Standard: Analytics engineering teams at Goldman Sachs enforce explicit casting on all joined attributes to eliminate silent implicit coercion and index disabling.

Real-World Example: ScoopCraft POS String-to-Numeric Ingestion Cleanup

ScoopCraft ingests raw POS dump files where unit price and sales tax were extracted as text strings.

Raw String ValueCasting SyntaxClean MySQL TypeMathematical Result
'350.50'CAST(price_str AS DECIMAL(10,2))DECIMAL(10,2)350.50 (Exact currency)
'42'CAST(units_str AS SIGNED)SIGNED INTEGER42 (Can perform math)
'2026-11-13'CAST(date_str AS DATE)DATE2026-11-13 (Calendar math allowed)

Syntax & Core SQL Implementation

-- Explicit Type Casting in MySQL
SELECT 
    order_id,
    -- Cast string to exact decimal
    CAST(raw_price AS DECIMAL(10,2)) AS unit_price,
    -- Cast string to integer
    CAST(raw_quantity AS SIGNED) AS qty,
    -- Cast integer to string
    CAST(order_id AS CHAR(20)) AS order_code_str,
    -- Cast string to date
    CAST('2026-11-13' AS DATE) AS delivery_date,
    -- CONVERT syntax alternative
    CONVERT(raw_price, DECIMAL(10,2)) AS converted_price
FROM staging_orders;

Parameters & Arguments Breakdown

  • CAST(expr AS type): Standard ANSI function converting expression to specified target type.
  • SIGNED / UNSIGNED: Target integer types in MySQL casting (signed allows negatives; unsigned is 0+).
  • DECIMAL(m, d): Target fixed-point numeric type for financial math.
  • CONVERT(expr, type): MySQL function equivalent to CAST(), also supports character set conversion.

Sample Walkthrough (Q&A)

Question: Why does "WHERE varchar_col = 100" run slowly even if varchar_col has an index in MySQL?

SQL Pattern: varchar_col = 100 (Implicit Coercion)

Result: MySQL converts varchar_col to number on every row, disabling the B-Tree index and causing a full table scan

Explanation: When comparing a string column to an integer constant, MySQL converts the string column to a float/number. To preserve index seeks, quote the constant: WHERE varchar_col = "100".

Common Mistakes & Limitations

  • Using `CAST(col AS INTEGER)`; in MySQL, the target cast type keyword must be `SIGNED` or `UNSIGNED` (not `INTEGER`).
  • Comparing string columns with numeric constants, triggering implicit coercion that disables indexes.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute QueryCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • In MySQL CAST syntax, the valid target types are: `BINARY`, `CHAR`, `DATE`, `DATETIME`, `TIME`, `DECIMAL`, `SIGNED`, `UNSIGNED`, `JSON`, and `DOUBLE` (MySQL 8.0.17+).
  • Always cast the literal value rather than the column in filters (`col = CAST(123 AS CHAR)`) to preserve column index seeks.