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: Today @ 4:28 AM
Points: 6,149, Visits: 8,407
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: Today @ 4:28 AM
Points: 6,149, Visits: 8,407
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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Monday, December 22, 2014 12:15 AM
Points: 9,928, Visits: 11,207
What Hugo said.



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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, 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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Monday, December 22, 2014 12:15 AM
Points: 9,928, Visits: 11,207
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 3:32 PM
Points: 18,068, Visits: 16,111
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
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: Thursday, November 20, 2014 4:41 PM
Points: 300, Visits: 319

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: Thursday, November 20, 2014 4:41 PM
Points: 300, Visits: 319

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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Monday, December 22, 2014 12:15 AM
Points: 9,928, Visits: 11,207
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