How do I round a number to two decimals in SQL commander in DBV?

I am dividing two numbers, where numerator is smaller than denominator. I was expecting some numbers between 0 and 1 but I always get 0 without any decimals. I would like to see at least one decimal. I tried the round function but it isn’t working for me. What should I do?

Edit: the two numbers are integers in millions.

I tried round function but it didn’t do the trick.

>Solution :

Most likely your numbers are integers, so if you try to divide them it will be integer division. To get around this you need to cast one (or both) of your numbers to a decimal data type:

SELECT CAST(your_numerator AS DECIMAL(10, 2)) / your_denominator AS rounded_value
FROM your_table;

The DECIMAL(10, 2) function means you can store numbers with up to 10 digits in total, where 2 of those digits are reserved for the decimal part, e.g. 12345.67 (10 digits in total, 2 decimal places)`. Obviously you can change these numbers if you want more decimal places.

Leave a Reply