Skip to content

Secondary keywords: mysql cast to int, mysql cast int to string, cast as int mysql Type: Blog Post

In MySQL, the CAST() function is used to convert data from one data type to another. When you need to convert a value to an integer data type, you can use CAST() to achieve this. Here’s how you can use CAST() to cast a value as an integer:

SELECT CAST(column_name AS SIGNED) AS integer_column_name
FROM your_table;

Replace column_name with the name of the column you want to cast, and your_table with the name of your table. The AS SIGNED keyword specifies that the value should be converted to a signed integer. If the value cannot be converted, MySQL will return NULL.

For example, if you have a column named price containing numeric values stored as strings, and you want to convert it to integers:

SELECT CAST(price AS SIGNED) AS integer_price
FROM products;

This query will return the price column values cast as integers under the alias integer_price.

Remember to ensure that the values in the column can actually be converted to integers; otherwise, you may encounter errors or unexpected results.

There you go! Using the CAST() function provides a straightforward way to convert data types within MySQL queries, making it easier to manipulate and work with different types of data as needed.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.