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


What will this return?


What will this return?

Author
Message
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13331 Visits: 12184
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
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13331 Visits: 12184
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
Paul White
Paul White
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20566 Visits: 11359
What Hugo said. :-)



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7210 Visits: 6903
I got it wrong. I answered "purple". Doze

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Paul White
Paul White
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20566 Visits: 11359
The Dixie Flatline (5/26/2010)
I got it wrong. I answered "purple". Doze

*sound of a mouthful of tea hitting an LCD screen at high speed*



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
SQLRNNR
SQLRNNR
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41215 Visits: 18565
The Dixie Flatline (5/26/2010)
I got it wrong. I answered "purple". Doze


Family Feud Style:

"Good Answer, Good Answer!!"Cool



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw

yidnek T Beyene
yidnek T Beyene
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 342
Put

select @hourDiff

at the end of the code and rerun it.
yidnek T Beyene
yidnek T Beyene
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 342
Put the below statment at the end of the code and rerun it.

select @hourDiff
Z.A.T
Z.A.T
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 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.
Paul White
Paul White
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20566 Visits: 11359
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
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