This example illustrates how to use the cast function and operator in mysql.
This example illustrates how to use the cast function and operator in mysql.This example illustrates how to use the cast function and operator in mysql.
In this example the Convert function is used to convert the data from one type to another type. The CONVERT() function is used to take a value of one type and produce a value of another type. As like Convert function the CAST() function is used to take data of one type and produced a value of another type. There are many more example of query is used in the mysql which produced result given below.
Query
|
SELECT CONVERT('abc' USING utf8);
|
Output
|
+---------------------------+ | CONVERT('abc' USING utf8) | +---------------------------+ | abc | +---------------------------+
|
Convert image in to binary.
Query
|
SELECT 'image' LIKE CONVERT(image USING binary) FROM pictures;
|
Output
|
+------------------------------------------+ | 'image' LIKE CONVERT(image USING binary) | +------------------------------------------+ | 0 | | 0 | | 0 | +------------------------------------------+
|
Cast in to integer to decimal.
Query |
SELECT CAST('19' AS DECIMAL(5,2)) as result; |
Output
|
+--------+ | result | +--------+ | 19.00 | +--------+ |
Cast the current Date.
Query
|
SELECT CAST(NOW() AS DATE);
|
Output
|
+---------------------+ | CAST(NOW() AS DATE) | +---------------------+ | 2009-01-16 | +---------------------+
|
Cast the current Date in to integer.
Query
|
SELECT CAST(NOW() AS SIGNED);
|
Output
|
+-----------------------+ | CAST(NOW() AS SIGNED) | +-----------------------+ | 2009 | +-----------------------+
|