# decimel help

Hello All,

I have a table where a column is gets updated by the division of a column from another table by any denominator value between 3 and 9. So it could be metric column / 3  or metriColumn/4 , etc.

Now in terms of the maximum of decimals for highest accuracy – s – what should be used? Wen creating that table to store the division result column – I am using –
CREATE TABLE [dbo].[myTable](
[divisionResults] [decimal](18, 4) NULL
) ON [PRIMARY]

I want to use storage type that allows me the maximum decimal places after zero but also doesn’t compromise the query efficiency or speed or that I am “overkilling” it
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MSSQL Senior EngineerCommented:
You can see the precision for decimal data type here and as far as I know the precision doesn't impact the query performance.
Director of Solutions ConsultingCommented:
One additional thing to rember is when you do math in sql with an int and a decimal, depending on the specific order of the numbers (decimal/int , int/decimal) you can get the result as an int when you want a decimal.
http://www.experts-exchange.com/Database/MS-SQL-Server/Q_24432624.html

I prefer to explicitly cast my int as a decimal but there are lots of ways.
Decimal / cast (int as decimal(18,4)
MSSQL Senior EngineerCommented:
depending on the specific order of the numbers (decimal/int , int/decimal) you can get the result as an int when you want a decimal.
That's not true. In both case the result will be always a decimal.
Only if the operation is int/int that the result will be int.
Director of Solutions ConsultingCommented:
whoops, that's right. I've just gotten in the habit of casting everything to avoid odd issues.
http://improve.dk/tricky-sql-server-decimal-math/
Author Commented:
[myResults] [decimal](18, 4) NULL
so whats the max number of decimel that i can use? instead of 4 like up till 12 or more?
Director of Solutions ConsultingCommented:
Author Commented:
please suggest a valid one - this is leading to arithmetic overflow
Author Commented:
all eexperts - please suggest whats your best practise of using which one decimel ? i dont want my program to crash
Director of Solutions ConsultingCommented:
in your example (18,4) that means 18 total digits, 4 to the right of the decimal point. Unless you care about more digits to the right of the decimal point, that should be fine for numbers up to 99 trillion (99,999,999,999,999.9999)
MSSQL Senior EngineerCommented:
all eexperts - please suggest whats your best practise of using which one decimel ? i dont want my program to crash
There's no best practice but client/business demands. What's the precision that they need? You just need this answer to set the best data type.
Commented:
As Aaron and Vitor say, you select your total number of digits and your number of decimal places within that, For example, if you're working with something like national expenditures then 18,4 might be good. OTOH, for chemical analyses something like 18,12 might be more appropriate.

As others have noted, use a decimal value as your denominator, if for no other reason than to make everything clear to subsequent readers of your code. If you are working with large amounts of data then every calculation will include an implicit conversion if you leave it as an integer. While this probably won't be as expensive as dividing by '4' (as a varchar or char) it will still incur some extra overhead.

hth

Mike

Experts Exchange Solution brought to you by