What is the method for converting numerical types in MySQL?
In MySQL, you can convert numeric types by using the CAST() function or directly converting the numeric field.
- Convert using the CAST() function:
SELECT CAST(column_name AS new_data_type) FROM table_name;
For example, converting an integer field to a float data type:
SELECT CAST(integer_column AS DECIMAL(10,2)) FROM table_name;
- Do the conversion directly.
SELECT column_name + 0 AS new_column_name FROM table_name;
For example, converting a string field to an integer type.
SELECT string_column + 0 AS integer_column FROM table_name;