?
Solved

SQL Server decimal calculations showing odd results

Posted on 2012-03-22
5
Medium Priority
?
477 Views
Last Modified: 2012-03-26
Consider the script below ran in Management Studio on SQLServer 2008:

          declare @N1 decimal(38,12)
          declare @N2 decimal(38,12)
          begin        
            set @N1=1111223333444455556666777.111222233
            set @N2=1111223333444455556666777.111222233
            select @N1/@N2  --expected result
            select @N1*cast(-1 as decimal(38,12))/@N2
            select @N1*(-1*(1.0))/@N2      
            select @N1*(-1.0/@N2)    
            select 1.0/@N2
          end;

The results are:
1.000000
-0.999999
-0.999999
-0.888979
0.0000000000000000000000008

Can anyone explain these results? Our problem is our software has to parse user friendly representation of field and number summing into sql friendly script which in turn gives correct and consistent results. We do need to deal with large number and many decimal places, hence using decimal(38,12). We are aware of the need to make the nominator of a division into a decimal number otherwise the result is assumed to be an integer,
ie
select 2/5 results in 0, and select 2.0/5 results in 0.4

We have also noticed that SQLServer doesn't always apply BODMAS calculation ordering. So, is there a set of rules that can be followed to achieve reliable results?

Many thanks
Alex
0
Comment
Question by:stbdevelopment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Assisted Solution

by:deighton
deighton earned 150 total points
ID: 37753166
I think you are overflowing the maximum size of a float
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 900 total points
ID: 37753285
Interesting...seems like the more precision you give, the less you get back.  

         declare @N1 decimal(38,12)
            set @N1=1.111122223333
            select @N1
            select @N1/1
            select @N1/1.0
          select @N1/1.00
          select @N1/1.000
            select @N1/1.00000000000000000
            select @N1/cast(1 as decimal(38,12))
            select @N1/cast(1 as decimal(38,3))

-------------------

1.111122223333

1.111122223333

1.11112222333

1.1111222233

1.111122223

1.111122

1.111122

1.111122223
0
 

Author Comment

by:stbdevelopment
ID: 37753495
Thank you dqmq. That really highlights the problem we are having. However what is the solution?
Do we need to restrict the numbers used in calculations to so many decimal places and then cast them to a larger number of decimal places? But then the results would be the same. I can't see a way out of this.
0
 
LVL 42

Accepted Solution

by:
dqmq earned 900 total points
ID: 37754117
I don't know.  Still trying to make sense of it:

For more weirdness, try this:


           declare @N1 decimal(38,12)
            set @N1=   1.111122223333
            select @N1
          select @N1/1.0
          select @N1/1.000000000000
            select 1.111122223333/1.000000000000
            select 1.111122223333/1.0
0
 
LVL 2

Assisted Solution

by:winston33
winston33 earned 150 total points
ID: 37758673
I'm sorry if this does not completely answer your question, but I generally complete the equation using the native datatypes (assuming the two numbers are using the same datatype) and then convert the result to the desired data type. The only statement below where this does not apply is the third because the "1" needs to be converted prior to the division. I cannot explain WHY this works, but I generally get the proper results. For example, using your example:

          declare @N1 decimal(38,12)
          declare @N2 decimal(38,12)
          begin        
            set @N1=1111223333444455556666777.111222233
            set @N2=1111223333444455556666777.111222233
            select CONVERT(DECIMAL(38,12), @N1/@N2)  --expected result            
            select CONVERT(DECIMAL(38,12), (@N1*-1)/@N2)            
            select CONVERT(DECIMAL(38,12), CONVERT(DECIMAL(38,12), 1)/@N2)
          end;


1.000000000000

-1.000000000000

0.000000000000 --> This is actually the correct result, I think, given the precision and scale.

Anyway, hope this helps a little.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question