Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

A round number Expand / Collapse
Author
Message
Posted Tuesday, May 4, 2010 2:00 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, November 17, 2014 4:22 PM
Points: 2,262, Visits: 5,421
Nice intuitive question and an equally impressive explanation, Mr.Black Cap. Learnt quite a few behind-the-scenes of datatype conversions and roundings!

Thanks!
Post #915129
Posted Tuesday, May 4, 2010 2:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 3:56 PM
Points: 6,086, Visits: 8,354
Christian Buettner-167247 (5/4/2010)
One important observation: This is an error caused within the client tool, not within SQL Server.


Good observation, Christian. The format of the error message, which looks quite different from errors that are thrown by SQL Server, is also an indication that there is a different source of the error.

Here is another repro that runs fine on all clients, to demonstrate that SQL Server itself actually does not have any issues with this code:

DECLARE @a sql_variant;
SET @a = ROUND(0.5,0);

-- no error
SELECT CAST(@a AS decimal(12,2));
-- error when running on SSMS
SELECT @a;
go

-- Or, the more direct approach
SELECT CAST(ROUND(0.5,0) AS decimal(12,2));
go




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #915140
Posted Tuesday, May 4, 2010 2:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
ra.shinde (5/3/2010)
I executed all three statement together. Still no error. Getting 1.0


Same here. Looks like another bad question. The explanation was good but the answer is still wrong.

I executed all three commands at one time and am using SQL 2005 with SQLDbx as the client. It produces Numeric(1,1) with an answer of 1.

I selected 1 as the correct answer and was told it was wrong.

Post #915144
Posted Tuesday, May 4, 2010 3:05 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:56 AM
Points: 2,842, Visits: 3,876
cengland0 (5/4/2010)
[quote]ra.shinde (5/3/2010)
Looks like another bad question.

I would not consider this a bad question - I assume that almost noone actually knew the behaviour differences and actual cause of the error in SSMS before reading the comments.

I definitively learned something again today, and therefore I consider this a good question.
In the end, this is to some extent unpredictable behaviour. Paul definitively did enough research & due diligence before posting this question - sometimes items only get caught in the field test when tested with a wide variety of tools & settings etc. This is the real life.

So definitively thumbs up from me for this QODT!


Best Regards,
Chris Büttner
Post #915148
Posted Tuesday, May 4, 2010 3:08 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:56 AM
Points: 2,842, Visits: 3,876
Thank you Hugo - feels good to get positive feedback from an expert

Best Regards,
Chris Büttner
Post #915149
Posted Tuesday, May 4, 2010 3:43 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:53 PM
Points: 9,928, Visits: 11,194
Hugo Kornelis (5/4/2010)
Good observation, Christian. The format of the error message, which looks quite different from errors that are thrown by SQL Server, is also an indication that there is a different source of the error.

This just gets more and more interesting. I have been able to reproduce the SSMS-only behaviour when connecting to a default instance of SQL Server 2005 from SSMS and SQLCMD. I found this in Books Online:


Important:
SQL Server Management Studio uses the Microsoft .NET Framework SqlClient for execution in regular and SQLCMD mode in Query Editor. When sqlcmd is run from the command line, sqlcmd uses the OLE DB provider. Because different default options may apply, you might see different behavior when you execute the same query in SQL Server Management Studio in SQLCMD Mode and in the sqlcmd utility.


So, I thought...that explains that. Except that it doesn't. I get a proper SQL-Server-generated error for all the tests presented so far when connected to a named instance of SQL Server 2008 - connecting from SSMS or SQLCMD! The error is:

.Net SqlClient Data Provider: Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type numeric.

The error is captured in a Profiler trace under the Errors and Warnings: User Error Message category, so this is definitely a SQL Server error. This does not happen when connected to SQL Server 2005 - the error definitely comes from the provider not SQL Server.

Here's a repro to try (notice no value is returned to the client - it is written to a temporary table):
BEGIN   TRY
SELECT ROUND(0.5, 0) AS a INTO #a;
END TRY
BEGIN CATCH
SELECT message = ERROR_MESSAGE(),
number = ERROR_NUMBER(),
line = ERROR_LINE(),
severity = ERROR_SEVERITY(),
state = ERROR_STATE();
END CATCH;
DROP TABLE #a;

That produces full error details when connected to 2008, but runs without error on 2005 - even from SSMS. Finally, to add to the evidence, this is a screenshot of the results of SELECT ROUND(0.5, 0) when run from SQLCMD against 2005 and 2008:



Answers on a postcard, please!




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi


  Post Attachments 
Round.gif (234 views, 9.60 KB)
Post #915166
Posted Tuesday, May 4, 2010 5:04 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 9, 2013 5:08 AM
Points: 1,123, Visits: 603
I got the answer correct for the wrong reason... This really surprises me and definitely learned a lot from this! Shows how important it is to always convert your data before doing calculations. To me, this is a great question. If you want questions without any discussion about the results, things will get real boring around here.

Ronald Hensbergen

Help us, help yourself... Post data so we can read and use it: http://www.sqlservercentral.com/articles/Best+Practices/61537/
-------------------------------------------------------------------------
2+2=5 for significant large values of 2
Post #915192
Posted Tuesday, May 4, 2010 6:36 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:53 PM
Points: 9,928, Visits: 11,194
Christian Buettner-167247 (5/4/2010)
Does not look like it is settings related.
Here is the code executed via each client tool:

Hey Christian,

Thanks for the awesome feedback there - but have you noticed that SQLCMD gets the answer wrong?
ROUND(0.5, 0) is "1.0" not ".0"! I got the same results in my tests (see the rather pretty screenshot).
This has to be a bug. If no-one comes up with a comprehensive answer, this is going on Connect.

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #915247
Posted Tuesday, May 4, 2010 6:37 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:53 PM
Points: 9,928, Visits: 11,194
r.hensbergen (5/4/2010)
I got the answer correct for the wrong reason... This really surprises me and definitely learned a lot from this! Shows how important it is to always convert your data before doing calculations. To me, this is a great question. If you want questions without any discussion about the results, things will get real boring around here.

Fantastic, thank you Ronald. That's exactly how I feel about it.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #915248
Posted Tuesday, May 4, 2010 6:50 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:53 PM
Points: 9,928, Visits: 11,194
Hugo Kornelis (5/4/2010)
Here is another repro that runs fine on all clients, to demonstrate that SQL Server itself actually does not have any issues with this code:
-- Or, the more direct approach
SELECT CAST(ROUND(0.5,0) AS decimal(12,2));


According to Jim Hogg (MSFT) on Connect, the error is raised by SQL Server, and is By Design:
https://connect.microsoft.com/SQLServer/feedback/details/364387/using-round-function-with-passing-numeric-expression-to-9-5-and-length-0




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #915255
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse