Ha! It worked this time, didn't it? That's how you can force the desired result in SQL.
There is one 'but'. What can we do if both numbers are given as columns, e.g.
hp/level? We need to use another trick: convert one type to another explicitly. This procedure is called casting and it uses the structure shown in the example below:
SELECT CAST(hp AS numeric)/level
CAST(column AS type) changes the column to the specified type.
In SQL standard there are three kinds of numeric data types:
- integer data types. There are types with names like
- exact numeric data types (decimal types). These are types with names like
- inexact numeric data types, with names like
Data types are very tricky in databases. Each database has its own names and ranges for numeric data types. To get accurate information about data types supported in your database check its documentation!
In this course whenever we're doing division of two integers, we'll always cast the numerator to a
NUMERIC data type. In your real-world application, you may need a different precision and a different casting (for example cast both numbers to