|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, March 25, 2013 5:30 AM
Points: 1,641,
Visits: 423
|
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,479,
Visits: 1,943
|
|
Nice question. Though the explanation isnt entirely accurate. Its not rounded, its truncated.
If the question had been, lets say
declare @r decimal(5,2) set @r= round(5/3.2,1,3) select @r
Then we would get 1.5 wheras a round would have given us 1.6 (change last parameter in Round to 0... or remove it all together).
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 2:37 PM
Points: 18,853,
Visits: 12,437
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 1:11 AM
Points: 1,114,
Visits: 1,209
|
|
tommyh (9/16/2010)
Nice question. Though the explanation isnt entirely accurate. Its not rounded, its truncated. If the question had been, lets say declare @r decimal(5,2) set @r= round(5/3.2,1,3) select @r
Then we would get 1.5 wheras a round would have given us 1.6 (change last parameter in Round to 0... or remove it all together).
Right, the 3rd parameter of round is the most important one. It's value 3 is not typical but greater than zero.
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 6:30 AM
Points: 9,410,
Visits: 6,495
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354,
Visits: 1,299
|
|
I got it right but hesitated to click the submit button. It was too easy and I was looking for the tricky part of the question. I've been bitten by so many QOTDs with an obscure typo or something else tricky.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 8:36 AM
Points: 2,681,
Visits: 2,423
|
|
| Good question. Round is a function with a few gotchas that it is important to know about and understand. Thanks.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 10:39 AM
Points: 1,527,
Visits: 1,564
|
|
cengland0 (9/17/2010) ... I've been bitten by so many QOTDs with an obscure typo or something else tricky.
I like composing questions that are on the tricky side. Must be because of all my bad experiences with certification exams.
If it's of any consolation, the editors here do have a limit to what they will tolerate for trick questions. I submitted one a few months ago that never got published.
I get it. The QOTDs are more about the learning experience than the certification experience.
I'm working on one now that's a little more tame...
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 9:27 AM
Points: 361,
Visits: 507
|
|
The hardest part of the question was figuring out what is the result of 5/3.1 IMHO the point of the question would make more sense if it had been changed to 5/3.0 as the result of the function would actually be affected by the third parameter of the ROUND function unlike in this question. To illustrate:
select Example = 'Your example' ,input_data = '5/3.1' ,Truncation = ROUND(1.612903, 1, 1) -- 1.600000 ,Rounding = ROUND(1.612903, 1, 0) -- 1.600000 union all select 'Better example' ,'5/3.0' ,ROUND(1.666666,1,1) -- 1.600000 ,ROUND(1.666666,1,0) -- 1.700000
Example input_data Truncation Rounding -------------- ---------- ------------ ---------- Your example 5/3.1 1.600000 1.600000 Better example 5/3.0 1.600000 1.700000
Regards, Hrvoje Piasevoli
Hrvoje Piasevoli
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 7:59 PM
Points: 1,491,
Visits: 3,010
|
|
honza.mf (9/17/2010)
tommyh (9/16/2010)
Nice question. Though the explanation isnt entirely accurate. Its not rounded, its truncated. If the question had been, lets say declare @r decimal(5,2) set @r= round(5/3.2,1,3) select @r
Then we would get 1.5 wheras a round would have given us 1.6 (change last parameter in Round to 0... or remove it all together). Right, the 3rd parameter of round is the most important one. It's value 3 is not typical but greater than zero. Just to be picky, it's not because the third parameter is greater than zero, but because it is not equal to zero. Here's the entry in BOL for SQL 2005:
Syntax ROUND ( numeric_expression , length [ ,function ] )
Arguments numeric_expression Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
length Is the precision to which numeric_expression is to be rounded. length must be tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal positions specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.
function Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
|
|
|
|