Ha! It worked this time, didn't it? That's how you can force the desired result in T-SQL.
There is one problem with this method: What if both numbers are given as columns, like
HP / Level? We need to use another trick: explicitly converting one column to another data type. This procedure is called casting, and it uses the structure shown below:
CAST(HP AS NUMERIC) / Level AS Result
CAST(column AS type) changes the column to the specified type. In T-SQL, there are three kinds of numeric data types:
- integer data types with names like
- exact numeric data types (i.e. decimal types) with names like
- inexact numeric data types with names like
Data types are very tricky in databases. Each database has its own name and ranges for numeric data types.
When dividing two integers in this course, we'll always cast the numerator to a
NUMERIC data type. In a real-world application, you may need a different precision and a different casting (e.g., casting both numbers to