302

I'm trying to figure out decimal data type of a column in the SQL Server. I need to be able to store values like 15.5, 26.9, 24.7, 9.8, etc

I assigned decimal(18, 0) to the column data type but this not allowing me to store these values.

What is the right way to do this?

0

8 Answers 8

544

DECIMAL(18,0) will allow 0 digits after the decimal point.

Use something like DECIMAL(18,4) instead that should do just fine!

That gives you a total of 18 digits, 4 of which after the decimal point (and 14 before the decimal point).

Sign up to request clarification or add additional context in comments.

12 Comments

@marc_s, Is there anyway to store a value "4.5" where the column datatype is decimal(4,2)? When I insert the value, it is getting modified to "4.50"
@ArunkumarTK: decimal(4,2) allows 2 digits before and 2 digits after the decimal point. "4.5" can be stored no problem - and numerically, "4.5" and "4.50" are identical
@ArunkumarTK: WHY should those be different?? Both are "four and a half" - the value IS THE SAME.And NO don't use a varchar to store a decimal value !!
I know this is an old discussion but wouldn't that be '4.05' and '4.5' in that case @ArunkumarTK.
Yeah, why is 18 so popular (e.g. the default (18,0)) when both 18 and 19 use 9 bytes?
|
148

You should use is as follows:

DECIMAL(m,a)

m is the number of total digits your decimal can have.

a is the max number of digits you can have after the decimal point.

1 Comment

It is usually denoted as DECIMAL (p,s) where p stands for precision (maximum allowed digits in a number) and s stands for scale (maximum allowed digits after the decimal point).
49

The settings for Decimal are its precision and scale or in normal language, how many digits can a number have and how many digits do you want to have to the right of the decimal point.

So if you put PI into a Decimal(18,0) it will be recorded as 3?

If you put PI into a Decimal(18,2) it will be recorded as 3.14?

If you put PI into Decimal(18,10) be recorded as 3.1415926535.

Comments

40

For most of the time, I use decimal(9,2) which takes the least storage (5 bytes) in sql decimal type.


Precision => Storage bytes

  • 1 - 9 => 5
  • 10-19 => 9
  • 20-28 => 13
  • 29-38 => 17

It can store from 0 up to 9 999 999.99 (7 digit infront + 2 digit behind decimal point = total 9 digit), which is big enough for most of the values.

Comments

12

You can try this

decimal(18,1)

The length of numbers should be totally 18. The length of numbers after the decimal point should be 1 only and not more than that.

Comments

7

In MySQL DB decimal(4,2) allows entering only a total of 4 digits. As you see in decimal(4,2), it means you can enter a total of 4 digits out of which two digits are meant for keeping after the decimal point.

So, if you enter 100.0 in MySQL database, it will show an error like "Out of Range Value for column".

So, you can enter in this range only: from 00.00 to 99.99.

1 Comment

Why this is relevant while the question specified clearly Microsoft SQL Server?
3

The other answers are right. Assuming your examples reflect the full range of possibilities what you want is DECIMAL(3, 1). Or, DECIMAL(14, 1) will allow a total of 14 digits. It's your job to think about what's enough.

Comments

1
request.input("name", sql.Decimal, 155.33)              // decimal(18, 0)
request.input("name", sql.Decimal(10), 155.33)          // decimal(10, 0)
request.input("name", sql.Decimal(10, 2), 155.33)       // decimal(10, 2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.