1. Type Conversion in MySQL
Type conversion is the process of converting a value from one data type to another. It is crucial when:
- Performing arithmetic operations on string-based numbers.
- Comparing values of different types.
- Formatting data for output or reporting.
MySQL offers two main functions for explicit type conversion:
CAST(expression AS data_type)CONVERT(expression, data_type)
Both achieve similar results but differ slightly in syntax and usage.
2. CAST(): Explicitly Converting Data Types
The CAST() function explicitly converts an expression from one type to another.
Syntax
CAST(expression AS data_type)
expression: The value or column to convert.data_type: The target data type (e.g.,CHAR,DATE,SIGNED,DECIMAL).
Example 1: Converting String to Integer
SELECT CAST('123' AS SIGNED) AS converted_number;
Explanation:
'123'is a string.CAST(... AS SIGNED)converts it to an integer.- Result:
123(numeric type), which can now be used in arithmetic operations.
Example 2: Converting Number to String
SELECT CAST(2026 AS CHAR) AS converted_text;
Explanation:
- Converts the number
2026into a string. - Result:
'2026'(text type). - Useful for concatenation or formatting in reports.
Example 3: Converting String to Date
SELECT CAST('2026-04-27' AS DATE) AS converted_date;
- Converts the string
'2026-04-27'to a proper DATE type. - Allows date arithmetic, like adding or subtracting days.
3. CONVERT(): Another Way to Change Data Types
The CONVERT() function is similar to CAST but offers additional flexibility in some contexts, especially for character set conversion.
Syntax
CONVERT(expression, data_type)
expression: The value or column to convert.data_type: Target type (likeCHAR,SIGNED,DECIMAL, etc.).
Example 1: Converting String to Integer
SELECT CONVERT('456', SIGNED) AS converted_number;
- Converts the string
'456'to a numeric type. - Works identically to
CASTin this case.
Example 2: Converting Number to String
SELECT CONVERT(2026, CHAR) AS converted_text;
- Converts numeric
2026to'2026'. - Can be used in concatenation with other strings.
Example 3: Using CONVERT for Character Set Conversion
SELECT CONVERT('Hello World' USING utf8) AS utf8_text;
- Converts text to a different character set (UTF-8 in this case).
- Useful when dealing with multilingual databases or exporting data.
4. Key Differences Between CAST and CONVERT
| Feature | CAST | CONVERT |
|---|---|---|
| Syntax | CAST(expression AS data_type) | CONVERT(expression, data_type) |
| Character Set Conversion | No | Yes (USING charset) |
| Standard Compliance | ANSI SQL standard | MySQL-specific with additional options |
| Use Cases | General type conversion | Type conversion + charset conversion |
Principle: Both functions convert values explicitly, preventing unexpected results during operations or comparisons. While CAST is standard SQL, CONVERT offers extra flexibility for character sets.
5. Practical Use Cases of Type Conversion
A. Calculating with String Numbers
SELECT CAST('100' AS DECIMAL) + CAST('50.5' AS DECIMAL) AS total;
Explanation:
- Converts string numbers
'100'and'50.5'to numeric types. - Performs addition:
100 + 50.5 = 150.5.
B. Formatting Dates as Strings
SELECT CONCAT('Today is ', CAST(CURDATE() AS CHAR)) AS message;
- Converts the date returned by
CURDATE()into a string for concatenation. - Output:
Today is 2026-04-27.
C. Ensuring Accurate Comparisons
SELECT *
FROM orders
WHERE order_id = CAST('101' AS SIGNED);
- Prevents type mismatch when
order_idis numeric but input is a string. - Ensures accurate query results.
6. Best Practices for Type Conversion in MySQL
- Use explicit conversion: Avoid relying on implicit type conversion to prevent unexpected results.
- Prefer
CASTfor portability:CASTfollows SQL standards and works in most relational databases. - Use
CONVERTfor character set conversions: Ideal for multilingual applications. - Validate data types: Ensure source data is compatible with the target type to avoid errors.