Hah! It worked this time, didn't it? That's how you can force the desired result in PostgreSQL.
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:
SELECT (hp::numeric) / level AS result
column::type changes the column to the specified type. You can do this also by using the
SELECT CAST(hp AS numeric) / level AS result
This function takes the name of the column to convert, the keyword
AS, and the new data type.
In PostgreSQL, there are three kinds of number data types:
- Integer data types with names like
- Exact number data types (i.e., decimal types) with names like
- Inexact number data types with names like
When dividing two integers in this course, we 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