SQL CAST and CONVERT
Sometimes a value has the wrong type for the job: numbers stored as text, dates arriving as strings, an average that needs fixed decimals. CAST and CONVERT change the type of a value inside a query. Knowing them also explains the strange results MySQL’s automatic conversion can produce.
- Convert values with
CAST(x AS type)andCONVERT(x, type) - Turn text into numbers and dates, and back
- Control precision with
DECIMAL(p,s) - Spot implicit conversion surprises
CAST syntax
CAST(expression AS type)CONVERT(expression, type)The target types MySQL accepts in CAST are: SIGNED, UNSIGNED, DECIMAL(p,s), DOUBLE, FLOAT, CHAR, DATE, TIME, DATETIME, YEAR, BINARY and JSON. Note: INT and VARCHAR are not valid targets in MySQL; use SIGNED and CHAR.
Text to number
SELECT CAST('42' AS SIGNED) AS to_int, CAST('19.99' AS DECIMAL(6,2)) AS to_decimal, CAST('3.7' AS SIGNED) AS truncated_int, '10' + 5 AS implicit;| to_int | to_decimal | truncated_int | implicit |
|---|---|---|---|
| 42 | 19.99 | 3 | 15 |
CAST('42' AS SIGNED)turns the text into the integer 42.DECIMAL(6,2)keeps two decimals.- Casting the text
'3.7'to an integer reads digits up to the.and gives 3, with a “truncated incorrect value” warning. (Casting the number3.7, without quotes, rounds to 4.) '10' + 5shows implicit conversion: MySQL silently converts the text for arithmetic and returns 15.
Sorting numbers stored as text
A common bug: codes stored in a VARCHAR sort alphabetically, so '10' comes before '9':
CREATE TABLE tickets (ticket_no VARCHAR(10));INSERT INTO tickets VALUES ('9'), ('10'), ('2'), ('100');SELECT ticket_no FROM tickets ORDER BY ticket_no;SELECT ticket_no FROM tickets ORDER BY CAST(ticket_no AS UNSIGNED);| ticket_no |
|---|
| 10 |
| 100 |
| 2 |
| 9 |
| ticket_no |
|---|
| 2 |
| 9 |
| 10 |
| 100 |
- The first sort compares text character by character:
'1'<'2'<'9', so 10 and 100 come first. - Casting to
UNSIGNEDsorts by numeric value: 2, 9, 10, 100. - The real fix is to store numbers in a numeric column.
Number to text
SELECT emp_id, CAST(emp_id AS CHAR) AS id_text, CONCAT('EMP-', emp_id) AS implicit_text, CHAR_LENGTH(CAST(salary AS CHAR)) AS salary_digitsFROM employeesLIMIT 3;| emp_id | id_text | implicit_text | salary_digits |
|---|---|---|---|
| 100 | 100 | EMP-100 | 6 |
| 101 | 101 | EMP-101 | 6 |
| 102 | 102 | EMP-102 | 5 |
CONCAT converts numbers to text automatically, so an explicit CAST is rarely needed there.
Text to date
SELECT CAST('2026-01-31' AS DATE) AS iso_text, CAST('2026-01-31 18:30:00' AS DATETIME) AS with_time, CAST('2026-01-31 18:30:00' AS DATE) AS date_only, STR_TO_DATE('31-01-2026', '%d-%m-%Y') AS other_format;| iso_text | with_time | date_only | other_format |
|---|---|---|---|
| 2026-01-31 | 2026-01-31 18:30:00 | 2026-01-31 | 2026-01-31 |
CAST(... AS DATE)understands the ISO formatYYYY-MM-DD.- Casting a datetime to
DATEdrops the time. - For any other layout use
STR_TO_DATEwith a format string (Date Functions).
DECIMAL for fixed precision
SELECT dept_id, AVG(salary) AS raw_avg, CAST(AVG(salary) AS DECIMAL(10,2)) AS avg_2dpFROM employeesWHERE dept_id IN (1, 2)GROUP BY dept_id;| dept_id | raw_avg | avg_2dp |
|---|---|---|
| 1 | 78400.0000 | 78400.00 |
| 2 | 69000.0000 | 69000.00 |
CONVERT and character sets
CONVERT(x, type) is MySQL’s alternative syntax for CAST. CONVERT(x USING charset) converts between character sets:
SELECT CONVERT('123', SIGNED) AS same_as_cast, CHARSET(CONVERT('café' USING latin1)) AS latin1_charset;| same_as_cast | latin1_charset |
|---|---|
| 123 | latin1 |
Implicit conversion surprises
When you compare text with a number, MySQL converts the text to a number. Text that does not start with digits becomes 0:
SELECT 'abc' = 0 AS text_equals_zero, '12abc' = 12 AS leading_digits, '1e3' = 1000 AS scientific;| text_equals_zero | leading_digits | scientific |
|---|---|---|
| 1 | 1 | 1 |
'abc'becomes 0, so'abc' = 0is true (1), with a warning.'12abc'becomes 12.'1e3'is read as scientific notation, 1000.
Comparing a VARCHAR column with a number (for example WHERE phone = 9845012345) converts every row’s value, cannot use an index, and can match unexpected rows. Compare text columns with quoted text.
Quick quiz
1. Which CAST is valid in MySQL?
MySQL uses SIGNED or UNSIGNED for integers and CHAR for text.
2. What does SELECT '10' + 5 return in MySQL?
MySQL converts the text to a number for arithmetic.
3. Why do the text values ’10’, ‘9’, ‘100’ sort as ’10’, ‘100’, ‘9’?
Alphabetical order compares the first character first.
CAST and CONVERT FAQ
What is the difference between CAST and CONVERT in MySQL?
CAST(x AS type) is standard SQL. CONVERT(x, type) does the same in MySQL, and CONVERT(x USING charset) also converts character sets.
How do I convert a string to a number in MySQL?
Use CAST(text AS SIGNED), CAST(text AS UNSIGNED) or CAST(text AS DECIMAL(p,s)). Adding 0 (text + 0) also works through implicit conversion.
How do I convert a string to a date in SQL?
CAST('2026-01-31' AS DATE) works for ISO dates. For other formats use STR_TO_DATE(text, format).
Why can’t I CAST to INT in MySQL?
MySQL’s CAST accepts SIGNED [INTEGER] and UNSIGNED [INTEGER] as integer targets, not INT. (MySQL 8.0.17 and later also accept CAST(x AS FLOAT) and DOUBLE.)