The CAST() and CONVERT() functions may be used to take a value of one type and produce a value of another type. Their syntax is:
CAST(expression AS type) CONVERT(expression,type) CONVERT(expr USING transcoding_name)
The type value can be one of the following:
BINARY
CHAR
DATE
DATETIME
SIGNED {INTEGER}
TIME
UNSIGNED {INTEGER}
CAST() and CONVERT() are available as of MySQL 4.0.2. The CHAR conversion type is available as of 4.0.6. The USING form of CONVERT() is available as of 4.1.0.
CAST() and CONVERT(... USING ...) are SQL-99 syntax. The non-USING form of CONVERT() is ODBC syntax.
The cast functions are useful when you want to create a column with a specific type in a CREATE ... SELECT statement:
CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);
The functions also can be useful for sorting ENUM columns in lexical order. Normally sorting of ENUM columns occurs using the internal numeric values. Casting the values to CHAR results in a lexical sort:
SELECT enum_col FROM tbl_name ORDER BY CAST(enum_col AS CHAR);
CAST(string AS BINARY) is the same thing as BINARY string. CAST(expr AS CHAR) treats the expression as a string with the default character set.
NOTE: In MysQL 4.0 the CAST() to DATE, DATETIME, or TIME only marks the column to be a specific type but doesn't change the value of the column.
In MySQL 4.1.0 the value is converted to the correct column type when it's sent to the user (this is a feature of how the new protocol in 4.1 sends date information to the client):
mysql> SELECT CAST(NOW() AS DATE); -> 2003-05-26
In later MySQL versions (probably 4.1.2 or 5.0) we will fix that CAST also changes the result if you use it as part of a more complex expression, like CONCAT("Date: ",CAST(NOW() AS DATE)).
You should not use CAST() to extract data in different formats but instead use string functions like LEFT or EXTRACT(). See Date and time functions.
To cast a string to a numeric value, you don't normally have to do anything; just use the string value as it would be a number:
mysql> SELECT 1+'1'; -> 2
If you use a number in string context, the number will automatically be converted to a BINARY string.
mysql> SELECT CONCAT("hello you ",2); -> "hello you 2"
MySQL supports arithmetic with both signed and unsigned 64-bit values. If you are using numerical operations (like +) and one of the operands is unsigned integer, the result will be unsigned. You can override this by using the SIGNED and UNSIGNED cast operators to cast the operation to a signed or unsigned 64-bit integer, respectively.
mysql> SELECT CAST(1-2 AS UNSIGNED) -> 18446744073709551615 mysql> SELECT CAST(CAST(1-2 AS UNSIGNED) AS SIGNED); -> -1
Note that if either operand is a floating-point value, the result is a floating-point value and is not affected by the above rule. (In this context, DECIMAL values are regarded as floating-point values.)
mysql> SELECT CAST(1 AS UNSIGNED) - 2.0; -> -1.0
If you are using a string in an arithmetic operation, this is converted to a floating-point number.
The handing of unsigned values was changed in MySQL 4.0 to be able to support BIGINT values properly. If you have some code that you want to run in both MySQL 4.0 and 3.23 (in which case you probably can't use the CAST() function), you can use the following technique to get a signed result when subtracting two unsigned integer columns:
SELECT (unsigned_column_1+0.0)-(unsigned_column_2+0.0);
The idea is that the columns are converted to floating-point values before the subtraction occurs.
If you get a problem with UNSIGNED columns in your old MySQL application when porting to MySQL 4.0, you can use the --sql-mode=NO_UNSIGNED_SUBTRACTION option when starting mysqld. Note however that as long as you use this, you will not be able to make efficient use of the BIGINT UNSIGNED column type.
CONVERT() with USING is used to convert data between different character sets. In MySQL, transcoding names are the same as the corresponding character set names. For example, this statement converts the string 'abc' in the server's default character set to the corresponding string in the utf8 character set:
SELECT CONVERT('abc' USING utf8);