|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 9:27 AM
Points: 361,
Visits: 507
|
|
Hugo Kornelis (8/24/2010)
Here is an interesting experiment (works on every client): SELECT ROUND(789.98,-3) AS WeirdCol INTO WeirdTable; go EXEC sp_help 'WeirdTable'; go UPDATE WeirdTable SET WeirdCol = WeirdCol - 0.001; go DROP TABLE WeirdTable; go
Hugo what is the expected result? On my system it's an empty table (nothing weird about the table though ) and the update operates on 0 rows - therefore nothing is really happening further. Are you referring to the fact that the table gets created despite the arithmetic overflow?
Regards,
Hrvoje Piasevoli
Hrvoje Piasevoli
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,529,
Visits: 359
|
|
Hugo Kornelis (8/24/2010)
ziangij (8/23/2010)
plz. refer http://technet.microsoft.com/en-us/library/ms175003.aspx for select ROUND(748.58, -3)
result displayed is 1000.00 but when i execute the same i get an error: An error occurred while executing batch. Error message is: Arithmetic Overflow.
any suggestions plz. ? I get the same error. The explanation is simple, and already given by tommyh: "748.58" is considered to be numeric(5,2), and the result of ROUND is the same data type as its input. But 1000.00 does not fit in a numeric(5,2). The fix is simple - use an explicit CAST to get a data type that does have some room for expansion: SELECT ROUND(CAST(748.58 AS decimal(6,2)), -3); tommyh (8/24/2010) With every setting at the default however SQL allows this. Now i havent been able to replicate the error.Frankly, this surprises me a lot more than the error ziangij gets. (And that I can replicate). As stated, I do get the same error message. (SQL 2005, SP3). What version did you test this on?
Microsoft SQL Server 2005 - 9.00.4294.00 (X64) Enterprise Edition (64-bit)
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,529,
Visits: 359
|
|
hrvoje.piasevoli (8/24/2010)
Hugo Kornelis (8/24/2010)
Here is an interesting experiment (works on every client): SELECT ROUND(789.98,-3) AS WeirdCol INTO WeirdTable; go EXEC sp_help 'WeirdTable'; go UPDATE WeirdTable SET WeirdCol = WeirdCol - 0.001; go DROP TABLE WeirdTable; go Hugo what is the expected result? On my system it's an empty table (nothing weird about the table though  ) and the update operates on 0 rows - therefore nothing is really happening further. Are you referring to the fact that the table gets created despite the arithmetic overflow? Regards, Hrvoje Piasevoli
yeah that's really strange... table is created.. select queries gives an arithmetic overflow error...
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:15 AM
Points: 2,864,
Visits: 2,467
|
|
Great QOD Hugo. Really makes you think.
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 19, 2011 7:26 AM
Points: 1,078,
Visits: 289
|
|
| Thanks Hugo. Great Question.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:46 AM
Points: 5,238,
Visits: 7,046
|
|
hrvoje.piasevoli (8/24/2010)
Hugo Kornelis (8/24/2010)
Here is an interesting experiment (works on every client): SELECT ROUND(789.98,-3) AS WeirdCol INTO WeirdTable; go EXEC sp_help 'WeirdTable'; go UPDATE WeirdTable SET WeirdCol = WeirdCol - 0.001; go DROP TABLE WeirdTable; go Hugo what is the expected result? On my system it's an empty table (nothing weird about the table though  ) and the update operates on 0 rows - therefore nothing is really happening further. Are you referring to the fact that the table gets created despite the arithmetic overflow? Regards, Hrvoje Piasevoli Hi Hrovje,
What version did you run this on? Maybe the problem has been fixed in SQL 2008?
On SQL Server 2005, the table gets created AND populated with a single row. In that row, the value 1000.00 is "somehow" stored in a numeric(5,2) column. The UPDATE then failes (because 1000.00 - 0.001 = 999.999; converted back to numeric(5,2) it rounds to 1000.00 again, and now it will not store the same value). You can even get the update to fail by using SET WeirdValue = WeirdValue - 0.0
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 2:19 AM
Points: 1,257,
Visits: 4,258
|
|
| Nope, just tested it on a SQL 2008 Express install and it came up with an arithmetic overflow as expected.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:46 AM
Points: 5,238,
Visits: 7,046
|
|
paul.knibbs (8/24/2010) Nope, just tested it on a SQL 2008 Express install and it came up with an arithmetic overflow as expected. Super! Thanks for testing and reporting back here. Obviously, this weird behaviour that appears to be exclusive to the ROUND() function has been fixed in SQL Server 2008.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 1:10 PM
Points: 2,673,
Visits: 2,418
|
|
| Interesting question....it was the -1 that made me stop and think for a minute
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 2:19 AM
Points: 1,257,
Visits: 4,258
|
|
Hugo Kornelis (8/24/2010) Super! Thanks for testing and reporting back here. Obviously, this weird behaviour that appears to be exclusive to the ROUND() function has been fixed in SQL Server 2008.
Is it a problem with the ROUND() function, or something else? It seems odd that when you select the content of WeirdTable before dropping it that you get the single 1000.00 value returned on SQL 2005--you would have thought it wouldn't allow you to insert that value in a DECIMAL(5,2) column in the first place, regardless of where it came from!
|
|
|
|