Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL Server decimal data type rounds up

I need to be able to store numbers like 3.5 in my table. So I've used the decimal type field. But if I enter 3.5 it round it up to 4. Am I being stupid or is it not the point of decimal to have a decimal point?

like image 890
iamjonesy Avatar asked Apr 06 '10 08:04

iamjonesy


People also ask

How do I stop rounding decimals in SQL?

The default scale of a decimal is zero, which means that what you have is actually an integer type. So, it's not possible to return the value 0.5 without changing the data type. If you specify a precision and scale for the type, like decimal(18,2) , it can handle numbers that are not integers.

How do you round up decimals in SQL?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places. Tip: Also look at the FLOOR() and CEILING() functions.

How does SQL Server handle decimals?

The Basic syntax of Decimal data type in SQL Server Where, p stands for Precision, the total number of digits in the value, i.e. on both sides of the decimal point. s stands for Scale, number of digits after the decimal point.

Does round in SQL round up or down?

SQL ROUND Function You might have known CEILING and FLOOR, but ROUND is by far the most common. Rounding just means to round up from 5 or down from anything less. ROUND is unique because you can tell SQL which position you would like rounded.


1 Answers

You need to declare it like decimal(18,3) to specify the number of digits after the point.

In case you are using stored procedure parameter also must have precision specified next to decimal, e.g. decimal(18,3)

like image 69
st78 Avatar answered Oct 21 '22 13:10

st78