Information turned into a Community Wiki answer from this question on Stack Overflow: What is the data type of a computed column?
It's determined by the value with the highest datatype precedence.
You can use
sql_variant_property to determine what datatype a literal expression is as per my answer here. e.g.
2147483648 is interpreted as
numeric(10,0) rather than
In addition, if you want to force a specific data type instead of relying on data type precedence, you can use
CAST in the computation to force it (assuming all potential outcomes are compatible with the type you choose). This can be very useful in cases where, by default, you end up with a wider data type than you intended; the most common use case I've seen is when you end up with an INT instead of, say, a BIT:
Active1 AS CASE WHEN something THEN 1 ELSE 0 END,
Active2 AS CONVERT(BIT, CASE WHEN something THEN 1 ELSE 0 END)
In this case
Active1 is an
INT (4 bytes) while
Active2 is a
BIT (1 byte - or less, potentially, if it is adjacent to other