SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A round number


A round number

Author
Message
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3669 Visits: 5548
Nice intuitive question and an equally impressive explanation, Mr.Black Cap. Learnt quite a few behind-the-scenes of datatype conversions and roundings!

Thanks!
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10945 Visits: 11984
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
cengland0
cengland0
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1676 Visits: 1300
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.
Christian Buettner-167247
Christian Buettner-167247
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3553 Visits: 3889
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
Christian Buettner-167247
Christian Buettner-167247
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3553 Visits: 3889
Thank you Hugo - feels good to get positive feedback from an expert :-)

Best Regards,

Chris Büttner
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15626 Visits: 11355
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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Attachments
Round.gif (246 views, 9.00 KB)
Ronald H
Ronald H
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 619
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
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15626 Visits: 11355
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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15626 Visits: 11355
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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15626 Visits: 11355
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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search