|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:37 PM
Points: 2,248,
Visits: 5,352
|
|
Nice intuitive question and an equally impressive explanation, Mr.Black Cap. Learnt quite a few behind-the-scenes of datatype conversions and roundings!
Thanks!
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
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
|
|
|
|
|
Ten 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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 6:12 AM
Points: 2,526,
Visits: 3,620
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 6:12 AM
Points: 2,526,
Visits: 3,620
|
|
Thank you Hugo - feels good to get positive feedback from an expert
Best Regards,
Chris Büttner
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 10,989,
Visits: 10,534
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, November 30, 2012 1:52 AM
Points: 1,116,
Visits: 602
|
|
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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 10,989,
Visits: 10,534
|
|
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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 10,989,
Visits: 10,534
|
|
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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 10,989,
Visits: 10,534
|
|
|
|
|