syntax
use cast:
cast (expression as data _ type)
use convert:
convert (data _ type [(length)], expression [,style])
parameter
expression
is any valid Microsoft SQL Server "expression. For more information, see Expressions.
data_type
the data types provided by the target system, including bigint and sql_variant. You cannot use user-defined data types. For more information about available data types, see Data Types.
length
optional parameters of nchar, nvarchar, char, varchar, binary or varbinary data types.
style
date format style, which can be used to convert datetime or smalldatetime data into character data (nchar, nvarchar, char, varchar, nchar or nvarchar data types); Or string format style, so as to convert float, real, money or smallmoney data into character data (nchar, nvarchar, char, varchar, nchar or nvarchar data types).
explicitly convert an expression of one data type to another data type. For more information about available data types, see Data Types. Date format style, so as to convert datetime or smalldatetime data into character data (nchar, nvarchar, char, varchar, nchar or nvarchar data types); Or string format style, so as to convert float, real, money or smallmoney data into character data (nchar, nvarchar, char, varchar, nchar or nvarchar data types). Output when converted into character data.
implicit conversion is invisible to users.
SQL Server automatically converts data from one data type to another. For example, if a smallint variable is compared with an int variable, the smallint variable is implicitly converted into an int variable before the comparison.
explicit conversion uses the CAST or CONVERT function.
the p>CAST and CONVERT functions convert numeric values from one data type (local variables, columns or other expressions) to another. For example, the following CAST function CONVERTs the value $157.27 into a string'' $157.27'': cast ($157.27 asvarchar (1))
The cast function is based on the SQL-92 standard and takes precedence over convert.
some implicit and explicit data type conversions are not supported when converting from the data type of one SQL Server object to another. For example, nchar values cannot be converted into image values at all. Nchar can only be explicitly converted to binary, and implicit conversion to binary is not supported. Nchar can be explicitly or implicitly converted into nvarchar.
when processing sql_variant data type, SQL Server supports implicit conversion of objects with other data types to sql_variant type. However, SQL Server does not support implicit conversion from sql_variant data to objects of other data types.