Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 What will this return? Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, May 26, 2010 12:57 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 10:10 AM Points: 5,597, Visits: 7,798
 bkubicek (5/25/2010)Hugo,It is the precision of the variable you are putting the value into that matter. @min is a numeric(10,4), so you will get your four decimal places using plain old 60.0.BenI assume you meant to write that @hourDiff is nummeric(10,4); @min is an integer. If that is what you meant, then you are right that the end result will always be converted to numeric(10,4). But intermediate results use a different precision; the casting to the target data type of numeric(10, 4) is the final step.Run the following code to see how the data types used for the operands of the division affect the length and precision of the intermediate result. In this case, since the division result is exactly 1.5, there will not be any net effect. But there are cases where the number of decimals used in the intermediate result may affect the end result. (Keep in mind that the division result will be truncated to the precision of the intermediate result, which will then be rounded to the precision of the variable).The difference will never be more than 0.0001, which admittedly is not significant in most cases. But in those cases where that difference is important, the difference between "60.0" and "CAST(60 AS numeric(10,4))" does matter. Hugo Kornelis, SQL Server MVPVisit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #927978
 Posted Wednesday, May 26, 2010 12:59 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 10:10 AM Points: 5,597, Visits: 7,798
 Rajasekhar Reddy (5/26/2010)Hi,select command in the code is used for assginign variables not to print any output.the given code won't return any value.Thanks,RajaSekhar Reddy .KThat is entirely correct. Of course, the question was "what result will @hourdiff hold?", not "what will be returned when you run this code", so I fail to see the significance of this observation. Hugo Kornelis, SQL Server MVPVisit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #927980
 Posted Wednesday, May 26, 2010 12:16 PM
 SSChampion Group: General Forum Members Last Login: 2 days ago @ 7:09 PM Points: 11,052, Visits: 10,818
 What Hugo said. Paul WhiteSQL Server MVPSQLblog.com@SQL_Kiwi
Post #928515
 Posted Wednesday, May 26, 2010 12:26 PM
 Hall of Fame Group: General Forum Members Last Login: Friday, November 29, 2013 10:10 AM Points: 3,896, Visits: 5,800
 I got it wrong. I answered "purple". __________________________________________________Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
Post #928524
 Posted Wednesday, May 26, 2010 12:29 PM
 SSChampion Group: General Forum Members Last Login: 2 days ago @ 7:09 PM Points: 11,052, Visits: 10,818
 The Dixie Flatline (5/26/2010)I got it wrong. I answered "purple". *sound of a mouthful of tea hitting an LCD screen at high speed* Paul WhiteSQL Server MVPSQLblog.com@SQL_Kiwi
Post #928532
 Posted Wednesday, May 26, 2010 12:36 PM
 SSCoach Group: General Forum Members Last Login: Friday, December 06, 2013 9:08 AM Points: 19,464, Visits: 13,052
 The Dixie Flatline (5/26/2010)I got it wrong. I answered "purple". Family Feud Style:"Good Answer, Good Answer!!" Jason AKA CirqueDeSQLeilI have given a name to my pain...MCM SQL Server 2008SQL RNNRPosting Performance Based Questions - Gail ShawPosting Data Etiquette - Jeff ModenHidden RBAR - Jeff ModenVLFs and the Tran Log - Kimberly Tripp
Post #928538
 Posted Wednesday, May 26, 2010 1:08 PM
 Old Hand Group: General Forum Members Last Login: Wednesday, February 13, 2013 3:27 PM Points: 300, Visits: 291
 Put select @hourDiff at the end of the code and rerun it.
Post #928566
 Posted Wednesday, May 26, 2010 1:18 PM
 Old Hand Group: General Forum Members Last Login: Wednesday, February 13, 2013 3:27 PM Points: 300, Visits: 291
 Put the below statment at the end of the code and rerun it.select @hourDiff
Post #928578
 Posted Thursday, May 27, 2010 4:13 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Tuesday, February 08, 2011 7:03 AM Points: 125, Visits: 51
 I run solved this query then I answerd NULL, because you forget to write select @hourDiff in your query.so my answer is write NULL. if you wrote select @hourDiff at the last line then it will return 1.
Post #928859
 Posted Thursday, May 27, 2010 4:18 AM
 SSChampion Group: General Forum Members Last Login: 2 days ago @ 7:09 PM Points: 11,052, Visits: 10,818
 Z.A.T (5/27/2010)I run solved this query then I answerd NULL, because you forget to write select @hourDiff in your query.so my answer is write NULL. if you wrote select @hourDiff at the last line then it will return 1.One might argue that the blame lies elsewhere, and the 'crime' committed was not reading the question properly.Attention to detail is often a desirable trait in DBAs...My other thought is that omitting the SELECT statement made it easier to get wrong for those people who just copy the code and execute it. Paul WhiteSQL Server MVPSQLblog.com@SQL_Kiwi
Post #928862

 Permissions