Converts a value to a different data type using the syntax CAST(expr AS type); this ANSI-standard T-SQL function errors out if the conversion can't be done.
Read more: Microsoft Learn
In the Ultra Transcenders books
Each book explains CAST in context, with comparison tables and the common traps.
Terms in this definition
- Data type
A rule limiting what a column may contain, for example INT for integers, DECIMAL for numbers with fractions or VARCHAR for text of varying length. Which types exist depends on the database product.
- T-SQL
The dialect of SQL that Microsoft uses for Azure SQL and SQL Server. Azure Monitor logs are queried with KQL instead.
Related terms
- ANSI mode
A stricter Spark SQL behaviour, enabled by default since Apache Spark 4.0 (so also in Fabric Runtime 2.0), where a bad cast or an integer overflow fails the query rather than quietly producing null. Functions such as
try_cast,try_addandtry_dividekeep the old null result, while switching ANSI mode off should be treated as a temporary workaround. - CONVERT
Works much like
CAST, changing a value's data type in T-SQL, with the addition of an optional style number; 126, for instance, gives ISO 8601 dates. - SQL code analysis
Checks run against a SQL database project when it is built, flagging problems such as SELECT * (SR0001), not using SCOPE_IDENTITY (SR0008) or a cast that might lose data (SR0014). Results appear as warnings unless you choose to treat them as errors.