MySQL Convert() Function
The CONVERT() function in MySQL is used to convert a value from one data type to the other data type specified in the expression. MySQL also allows it to convert a specified value from one character set to the different character set.
The following are the data types on which this function works perfectly:
The following are the syntax of CONVERT() function in MySQL:
This syntax accepts the following parameters, which are going to be discussed below:
It will return a value in which data type or character set we want to convert.
MySQL version support
This function can support the following versions:
Let us understand the MySQL CONVERT() function with the following examples. We can use the CONVERT function with the SELECT statement directly.
When we implement the statement, it will convert the value into DATETIME datatype.
When we implement the statement, it will convert the value into UNSIGNED datatype.
When we implement the statement, it will convert the value into SIGNED datatype.
When we implement the statement, it will convert the string value into an utf8mb4 character set.
Sometimes there is a need to convert a string between different character sets. In that case, we use the following statement for conversion:
The following statement first converts an integer value into string datatype and then perform concatenation with another specified string.
In this example, we are going to see how the CONVERT function works with the table. Let us first create a table "Orders" that has the following data:
In the above table, the Order_Date is in DATE datatype. So if we want to get a product name between selected ranges of time, execute the statement below.
We will get the following output:
Difference between CAST AND CONVERT Function
The main difference between CAST function and CONVERT function summarized in the below table: