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 «««23456»»

What will this return? Expand / Collapse
Author
Message
Posted Wednesday, May 26, 2010 12:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
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.

Ben

I 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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #927978
Posted Wednesday, May 26, 2010 12:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
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 .K

That 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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #927980
Posted Wednesday, May 26, 2010 12:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Saturday, August 30, 2014 4:20 PM
Points: 11,194, Visits: 11,142
What Hugo said.



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #928515
Posted Wednesday, May 26, 2010 12:26 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 4,013, Visits: 6,098
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

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Saturday, August 30, 2014 4:20 PM
Points: 11,194, Visits: 11,142
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 White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #928532
Posted Wednesday, May 26, 2010 12:36 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Friday, August 29, 2014 1:51 PM
Points: 21,644, Visits: 15,317
The Dixie Flatline (5/26/2010)
I got it wrong. I answered "purple".


Family Feud Style:

"Good Answer, Good Answer!!"




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #928538
Posted Wednesday, May 26, 2010 1:08 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 9:10 AM
Points: 300, Visits: 309

Put

select @hourDiff

at the end of the code and rerun it.
Post #928566
Posted Wednesday, May 26, 2010 1:18 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 9:10 AM
Points: 300, Visits: 309

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

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, February 8, 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

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Saturday, August 30, 2014 4:20 PM
Points: 11,194, Visits: 11,142
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 White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #928862
« Prev Topic | Next Topic »

Add to briefcase «««23456»»

Permissions Expand / Collapse