The data type of the requireDate column is DATE, therefore, MySQL has to convert the literal strings: '' and '' into TIMESTAMP values before evaluating the WHERE condition. The query selects orders whose required dates are in January 2003. WHERE requiredDate BETWEEN '' AND '' Code language: SQL (Structured Query Language) ( sql ) See the following query: SELECT orderNumber, Let’s take a look at the orders table in the sample database. The following statement explicitly converts an integer into a string and concatenate the string with another string: SELECT CONCAT( 'MySQL CAST example #', CAST( 2 AS CHAR)) Code language: SQL (Structured Query Language) ( sql ) To explicitly convert a string into an integer, you use the CAST() function as the following statement: SELECT ( 1 + CAST( '1' AS UNSIGNED))/ 2 Code language: SQL (Structured Query Language) ( sql ) In the following example, MySQL converts a string into an integer implicitly before doing calculation: SELECT ( 1 + '1')/ 2 Code language: SQL (Structured Query Language) ( sql ) Let’s take a look at some examples of using the CAST() function. The CAST() function is often used to return a value with a specified type for comparison in the WHERE, JOIN, and HAVING clauses. The target type can be any one of the following types: BINARY, CHAR, DATE, DATETIME, TIME, DECIMAL, SIGNED, UNSIGNED. The CAST() function converts a value of any type into a value that has a specified type. The syntax of the MySQL CAST() function is as follows: CAST(expression AS TYPE) Code language: SQL (Structured Query Language) ( sql ) Summary: in this tutorial, we will show you how to use MySQL CAST function to convert a value of any type into a value with a specified type.
0 Comments
Leave a Reply. |