MySQL Converting Expression

MySQL Convert function is used to convert the data from one type to another type. The CONVERT () function helps you to take a value of one type and result a value of another type.

MySQL Converting Expression

MySQL Converting Expression

     

MySQL Convert function is used to convert the data from one type to another type. The CONVERT () function helps you to take a value of one type and result a value of another type.

This example illustrates how to use the cast function and operator in mysql.

The Tutorial illustrate an example from 'MySQL Converting Expression'. To understand and grasp the example we use a query CAST() that accept data of one type and produced the value in another type.

 

 

 

Query

 

SELECT CONVERT('abc' USING utf8);

 

Output

 

+---------------------------+
| CONVERT('abc' USING utf8) |
+---------------------------+
| abc                       |
+---------------------------+

 

CONVERT(image USING binary) :This is used to Convert image in to binary as image 

Query

 

SELECT 'image' LIKE CONVERT(image USING binary) FROM pictures;

 

Output

 

+------------------------------------------+
| 'image' LIKE CONVERT(image USING binary) |
+------------------------------------------+
| 0                                        |
| 0                                        |
| 0                                        |
+------------------------------------------+

 

CAST('19' AS DECIMAL(5,2)) : This is used to Cast in to integer to decimal.

Query
 
SELECT CAST('19' AS DECIMAL(5,2)) as result;
 
Output

 

+--------+
| result |
+--------+
| 19.00  |
+--------+
 

CAST(NOW()) : This is used to Cast the current Date.

Query

 

SELECT CAST(NOW() AS DATE);

 

Output

 

+---------------------+
| CAST(NOW() AS DATE) |
+---------------------+
| 2009-01-16          |
+---------------------+

 

CAST(NOW() ) : Cast the current Date in to integer.

Query

 

SELECT CAST(NOW() AS SIGNED);

 

Output

 

+-----------------------+
| CAST(NOW() AS SIGNED) |
+-----------------------+
| 2009                  |
+-----------------------+