CAST() in MySQL — How It Works

What the MySQL CAST() function does, with an example.

Published October 22, 2026

CAST(expr AS type) converts a value to another data type such as SIGNED, UNSIGNED, DECIMAL(10,2), CHAR, DATE or JSON.

How it works

It is standard SQL and portable. CONVERT(expr, type) is the MySQL alternative.

Try it in MySQL

SELECT CAST('123' AS UNSIGNED)
123
SELECT CAST(3.567 AS DECIMAL(5,1))
3.6
SELECT CAST('2026-11-12' AS DATE)
2026-11-12

Practical examples

Example 1

SELECT CAST('123' AS UNSIGNED);
-- 123

Example 2

SELECT CAST(3.567 AS DECIMAL(5,1));
-- 3.6

Example 3

SELECT CAST('2026-11-12' AS DATE);
-- 2026-11-12

Use it in a real query

SELECT id, CAST('123' AS UNSIGNED) AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Invalid conversions giving 0 with warnings.
  • Casting indexed columns in WHERE.
  • Truncation of precision.

Key takeaways

  • CAST() in one line: CAST(expr AS type) converts a value to another data type such as SIGNED, UNSIGNED, DECIMAL(10,2), CHAR, DATE or JSON.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

CAST vs CONVERT?

CAST is standard SQL; CONVERT also converts character sets.

Does CAST() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles