MySQL

MySQL CAST: A Guide to the Data Conversion Function

intro

Let’s explore everything you need to know about the MySQL CAST function for seamless conversion between different data types.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

In computer science, casting refers to the process of converting a value from one data type to another. As you can imagine, that operation is also common in databases. That is why most DBMS technologies—including MySQL—come with dedicated data casting and conversion operators. Enter the MySQL CAST function!

In this article, you will learn what the CAST MySQL function is, how it works, and how to use it via multiple examples covering different conversion scenarios.

Let’s dive in!

What Is CAST in MySQL?

CAST is a MySQL function that takes an expression of any type and produces a result value of the specified type. In other words, it converts a value from one data type to another. Thus, the MySQL CAST function is used to change the data type of an expression or column in a query.

Keep in mind that the CAST MySQL function is an implementation of the standard SQL CAST. On top of that, MySQL also provides an equivalent CONVERT function, which offers the same functionality as CAST.

CAST MySQL Function: Syntax and Supported Types

Here is the syntax of CAST in MySQL:

Copy
        
1 CAST(expr AS type)

Where:

  • expr is the MySQL expression of any type (e.g., a value or column name) to cast to the specified type data type.
  • type is the target MySQL data type to convert expr to.

Note that if expr is NULL, CAST will always return NULL.

The allowed values for type are:

  • BINARY[(N)]: Produces a string of the VARBINARY data type. The optional length N causes the cast to use no more than N bytes of the argument. Values shorter than N bytes are padded with 0x00 bytes to a length of N. If N is not set, MySQL calculates the maximum length from the expression.
  • CHAR\[(N)\] [charset_info]: Produces a string with the VARCHAR data type. The optional length N causes the cast to use no more than N characters of the argument. No padding occurs for values shorter than N characters. If the optional length N is not given, CAST calculates the maximum length from the expression. If the supplied or calculated length is greater than an internal threshold, the result type is TEXT. If the length is still too long, the result type is LONGTEXT. If charset_info is not specified, MySQL produces a string with the default character set.
  • DATE: Produces a DATE value.
  • DATETIME[(M)]: Produces a DATETIME value. The optional M value defines the fractional precision in seconds. For more information about the difference between DATE and DATETIME, read our guide on SQL date data types.
  • DECIMAL[(M[,D])]: Produces a DECIMAL value. The optional M and D values specify the maximum number of digits (the precision) and the number of digits following the decimal point (the scale). If D is omitted, 0 is assumed. If M is omitted, 10 is assumed.
  • DOUBLE: Produces a result of the DOUBLE data type.
  • FLOAT[(p)]: If the precision p is not specified, produces a result of type FLOAT. If p is provided and 0 <= < p <= 24, the result is of type FLOAT. If 25 <= p <= 53, the result is of type DOUBLE. If p < 0 or p > 53, MySQL returns an error.
  • JSON: Produces a JSON value.
  • NCHAR[(N)]: Like CHAR, but produces a string with the national character set.
  • REAL: Produces a result of type REAL. If the REAL_AS_FLOAT SQL mode is enabled, CAST returns a FLOAT. Otherwise, the result is of type DOUBLE.
  • SIGNED [INTEGER]: Produces a signed BIGINT value.
  • Geometry spatial types like POINT, POLYGON, LINESTRING, etc.
  • TIME[(M)]: Produces a TIME value. The optional M value defines the fractional precision in seconds.
  • UNSIGNED [INTEGER]: Produces an unsigned BIGINT value.
  • YEAR: Produces a YEAR value.

For a complete reference, consult the official documentation.

In InnoDB, the syntax of the CAST MySQL function is:

Copy
        
1 CAST(expr AS type [ARRAY])

The optional ARRAY keyword is used to create a multi-valued index on a JSON array in CREATE INDEX, CREATE TABLE, and ALTER TABLE statements. Note that ARRAY is only applicable in these scenarios. When specified, the data type specified after the AS keyword can be any type supported by the CAST function, except for BINARY, JSON, and YEAR.

MySQL CAST In Action

Now that you know how to use the MySQL CAST function, you are ready to see it in action in some examples.

Since determining the data type of an SQL expression is not always straightforward, we will use DbVisualizer. This powerful database client allows you to easily identify the data type of a cell by simply clicking on it:

Note the Number:Unformatted type
Note the Number:Unformatted type

The value 818 could be either a string or a number. Thanks to the information displayed by DbVisualizer in the lower-left corner, you can confirm that it is a number. This is just one of the many powerful features offered by DbVisualizer.

Let’s explore some CAST MySQL examples!

Float to Int

You have the decimal value 62.73 and want to convert it to an integer. Here is how you can do it in a MySQL cast as int example:

Copy
        
1 SELECT CAST(62.73 AS SIGNED);

The result will be the integer number 63 :

Note that 63 is marked as a number
Note that 63 is marked as a number

Note that MySQL automatically rounds the number to the nearest integer.

Int to Float

When performing arithmetic calculations, one may have to convert an integer to a decimal to avoid truncation. Cast an integer to a decimal in MySQL as below:

Copy
        
1 SELECT CAST(8 AS DECIMAL);

The result will be the decimal representation of 8 :

Note that the result is marked as a decimal number
Note that the result is marked as a decimal number

As shown in DbVisualizer, this confirms that 8 is a decimal number.

String to Int

Suppose we have a number in a string and want to convert it to an integer for use in arithmetic operations. You can achieve that as in the MySQL cast string to int example below:

Copy
        
1 SELECT CAST ("123" AS UNSIGNED);

The result will be the number 123 :

Note that 123 is marked as a number
Note that 123 is marked as a number

Int to String

There are scenarios where you want to convert integers to strings. That is useful as MySQL offers a lot of functions and operators for dealing with string values. You can achieve this goal by using the following query clause:

Copy
        
1 SELECT CAST(42 AS CHAR)

The result will be the string "42" because we are considering 42 to be a character (string) value:

Note that the result is marked as text
Note that the result is marked as text

String to Date

Another common scenario is to convert a string to a date. You can do that with the CAST MySQL function, as in the example below:

Copy
        
1 SELECT CAST("2024-05-18" AS DATE);

The result will be the date 2025-05-18 :

Note that the result is marked as a date
Note that the result is marked as a date

If you write an invalid date string, the result will be NULL :

Note that the invalid string date is converted to NULL
Note that the invalid string date is converted to NULL

Timestamp to Date

Assume you have a timestamp and want to convert it to its corresponding date. Below is how you can do it:

Copy
        
1 SELECT CAST("2025-10-18 14:30:00" AS DATE);

The result is the date 2025-10-18:

Note that the result is a date
Note that the result is a date

Et voilà! You are now a master of CAST in MySQL.

Conclusion

In this guide, you saw what the CAST MySQL function is and how it works. You now know that CAST is a popular MySQL data type conversion function. Thanks to the examples shown here, you have also learned how and when to use it.

Figuring out the type of an SQL value can often be challenging, but with a robust database client like DbVisualizer, it becomes a breeze!

By simply clicking on a cell, you can instantly see the data type of the value it contains. This is just one of the many features offered by this powerful tool, which also includes advanced query optimization and automatic ERD-like schema generation. Try DbVisualizer for free today!

FAQ

Is the CAST MySQL function part of the ISO/ANSI standard?

Yes, CAST is part of the SQL standard defined by ISO/ANSI. The function is widely supported across various databases, ensuring compatibility and consistency in SQL syntax. The purpose of the function is to allow the conversion of a value from one data type to another.

Is it possible to MySQL cast as integer?

Yes, it is possible to MySQL cast as integer thanks to the CAST function. In particular, you can convert any data type to an integer by specifying SIGNED for signed integers or UNSIGNED for unsigned integers, respectively.

What is a cast as integer MySQL example?

Here is cast as integer MySQL:

Copy
        
1 SELECT CAST(82.91 AS SIGNED);

This will result in 83, as MySQL automatically rounds the value to the nearest integer.

What is a MySQL cast int to string example?

Below is a MySQL cast int to string example:

Copy
        
1 SELECT CAST(187 AS CHAR);

This will convert the integer 187 into the string "187".

What is the difference between CAST and CONVERT in MySQL?

Syntax is the difference. Both CAST and CONVERT in MySQL are used for data type conversion, but they differ slightly in syntax. CAST uses the syntax CAST(value AS type), while CONVERT uses CONVERT(value, type). Note that CONVERT does not support the creation of multi-value indexes as you can do with CAST in InnoDB by using the optional ARRAY keyword.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18
title

Introducing MariaDB Catalogs: What They Are and How They Work

author Lukas Vileikis tags MIGRATION MySQL SECURITY SQL 5 min 2024-11-13
title

MySQL CONCAT Function: The Definitive Guide

author Antonello Zanini tags MySQL 6 min 2024-11-11
title

SQL GROUP BY Clause: What It Is, How It Works, How to Approach It

author Lukas Vileikis tags DbVisualizer MySQL SQL 6 min 2024-11-06
title

SQL EXPLAIN: The Definitive Tool to Optimize Queries

author Antonello Zanini tags MySQL POSTGRESQL SQL 12 min 2024-10-29

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗