12.8. Cast Functions and Operators

MySQL 5.0

12.8. Cast Functions and Operators

  • The operator casts the string following it to a binary string. This is an easy way to force a column comparison to be done byte by byte rather than character by character. This causes the comparison to be case sensitive even if the column isn't defined as or . also causes trailing spaces to be significant.

    mysql> 
            -> 1
    mysql> 
            -> 0
    mysql> 
            -> 1
    mysql> 
            -> 0
    

    In a comparison, affects the entire operation; it can be given before either operand with the same result.

    is shorthand for AS BINARY).

    Note that in some contexts, if you cast an indexed column to , MySQL is not able to use the index efficiently.

  • AS ), ,), USING )

    The and functions take a value of one type and produce a value of another type.

    The can be one of the following values:

    • )]

    • )]

    produces a string with the data type. See Section 11.4.2, “The and Types” for a description of how this affects comparisons. If the optional length is given, ) causes the cast to use no more than bytes of the argument. As of MySQL 5.0.17, values shorter than bytes are padded with bytes to a length of .

    ) causes the cast to use no more than characters of the argument.

    The type is available as of MySQL 5.0.8.

    and are standard SQL syntax. The non- form of is ODBC syntax.

    with 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 in the default character set to the corresponding string in the character set:

    SELECT CONVERT('abc' USING utf8);
    

Normally, you cannot compare a value or other binary string in case-insensitive fashion because binary strings have no character set, and thus no concept of lettercase. To perform a case-insensitive comparison, use the function to convert the value to a non-binary string. If the character set of the result has a case-insensitive collation, the operation is not case sensitive:

SELECT 'A' LIKE CONVERT( USING latin1) FROM ;

To use a different character set, substitute its name for in the preceding statement. To ensure that a case-insensitive collation is used, specify a clause following the call.

can be used more generally for comparing strings that are represented in different character sets.

The cast functions are useful when you want to create a column with a specific type in a statement:

CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);

The functions also can be useful for sorting columns in lexical order. Normally, sorting of columns occurs using the internal numeric values. Casting the values to results in a lexical sort:

SELECT  FROM  ORDER BY CAST( AS CHAR);

AS BINARY) is the same thing as . AS CHAR) treats the expression as a string with the default character set.

also changes the result if you use it as part of a more complex expression such as .

You should not use to extract data in different formats but instead use string functions like or . See Section 12.5, “Date and Time Functions”.

To cast a string to a numeric value in numeric context, you normally do not have to do anything other than to use the string value as though it were a number:

mysql> 
       -> 2

If you use a number in string context, the number automatically is converted to a string.

mysql> 
        -> 'hello you 2'

MySQL supports arithmetic with both signed and unsigned 64-bit values. If you are using numeric operators (such as or ) and one of the operands is an unsigned integer, the result is unsigned. You can override this by using the and cast operators to cast the operation to a signed or unsigned 64-bit integer, respectively.

mysql> 
        -> 18446744073709551615
mysql> 
        -> -1

Note that if either operand is a floating-point value, the result is a floating-point value and is not affected by the preceding rule. (In this context, column values are regarded as floating-point values.)

mysql> 
        -> -1.0

If you are using a string in an arithmetic operation, this is converted to a floating-point number.

If you convert a “zero” date string to a date, and return when the SQL mode is enabled. As of MySQL 5.0.4, they also produce a warning.