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.
- FUNCTION
Registered models count as functions in Unity Catalog, so this securable type governs them too: access is given through
GRANT ... ON FUNCTION, and REST calls passFUNCTIONassecurable_type.
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. - ANSI mode
When on, Spark SQL stops returning null or wrapped values for bad casts, dividing by zero and integers that overflow, and throws an error as the query runs. Rather than disabling it, Databricks advises
try_divide,try_addortry_cast, each of which gives null on purpose. - CAST
Changes an expression's data type, written
CAST(expr AS type)or with the::operator. When ANSI mode is on, a conversion that fails is an error;try_castgives null in that case. - COALESCE
A SQL function that picks out the first non-NULL argument. CEL validation rejects a STRING cast inside the call, so a service policy function hands it the VARIANT field and casts what comes back.
- 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.
- variant_get
Written
variant_get(v, path, type), it follows a path into aVARIANTand returns the value cast to the chosen type.