Data type conversion and CAST function page 1 |
||
Implicit data type conversions are possible in Example 5.9.1 Find the average price of laptops with the prefix text "Average price = ". If you run the query the following error message will be obtained:Implicit conversion from data type varchar to money is not allowed. Use the CONVERT function to run this query. This message implies that the system cannot accomplish implicit conversion of the character constant "Average price = " to data type money of the price column. In such cases, explicit type conversion can help. In so doing, as the above message says, you can use the CONVERT function. However this function is not based on the SQL-92 standard. Because of this, it is preferable to use the standard function CAST. So, we start with CAST. If we rewrite above query as follows we get desired result:
We have used the expression for explicit type conversion CAST to bring the average value to the character view. The CAST statement has very simple syntax:
Attention:
Firstly, it should be noted that some data type conversions are not supported. (SQL-92 Standard involves the table of allowed conversions). Secondly, NULL value is converted into NULL value also. |