MySQL CAST() FunctionThe CAST() function in MySQL is used to convert a value from one data type to another data type specified in the expression. It is mostly used with WHERE, HAVING, and JOIN clauses. This function is similar to the CONVERT() function in MySQL. The following are the datatypes to which this function works perfectly:
SyntaxThe following are the syntax of CAST() function in MySQL: Parameter ExplanationThis syntax accepts two parameters, which are going to be discussed below:
Return ValueAfter conversion, it will return a value in which data type we want to convert. MySQL version supportThe CAST function can support the following MySQL versions:
Let us understand the MySQL CAST() function with the following examples. We can use the CAST function with the SELECT statement directly. Example 1This statement converts the value into DATE datatype. Output Example 2This statement converts the value into SIGNED datatype. Output Example 3This statement converts the value into UNSIGNED datatype. Output Example 4Sometimes there is a need to convert the string into an integer explicitly, use the following statement for converting the value into INTEGER datatype. Output Example 5The following statement first converts an integer value into string datatype and then perform concatenation with another specified string. Output Example 6In this example, we are going to see how the CAST function works with the table. Let us first create a table "Orders" that contains the following data: In the above table, we can see that the Order_Date is in DATE datatype. Now, if we want to get a product name between selected ranges of time, execute the statement below. Here, the literal string converted into timestamp value before evaluating the WHERE condition. This statement will produce the following output: Next TopicMySQL convert |