Data type conversion and CAST function page 1
Implicit data type conversions are possible in SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL implementations. For example, if a smallint is compared to an int in T-SQL, the smallint is implicitly converted to int before the comparison proceeds. See Электронная документация SQL Server Books OnlineBOL (SQL Server Books Online - electronic documentation and tutorials) for details about implicit and explicit conversions in MS SQL Server.
Find the average price of laptops with the prefix text "Average price = ".
If you run the query
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:
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.