order of evaluation in DateAdd with multiple expressions

15 pts.
Tags:
SQL Server DateTime
SQL Server Functions
I've spent 3 hours researching how order of evaluation would work in this expression. If @BeginTime was 16:45:00 for example, what would be the result of this expression? set @BeginTime = DateAdd(mi,-Datepart(mi,@BeginTime)-Datepart(hh,@BeginTime)*60,@BeginTime )

Answer Wiki

Thanks. We'll let you know when a new response is added.

It should give you ’00:00:00′

Lets split it into smaller parts:

this is the original expression:
<pre>set @BeginTime = DateAdd(mi,-Datepart(mi,@BeginTime)-Datepart(hh,@BeginTime)*60,@BeginTime ) </pre>

Now, let’s suppose we make
<pre>A = Datepart(mi,@BeginTime)</pre>
and
<pre>B = Datepart(hh,@BeginTime)</pre>

So, our main expression is now:
<pre>set @BeginTime = DateAdd(mi,-A-B*60,@BeginTime )</pre>
which, is the equivalent to:
<pre>set @BeginTime = DateAdd(mi,-A-<b>(</b>B*60<b>)</b>,@BeginTime )</pre>

Now, let’s calculate A and B.
<pre>A = Datepart(mi,’16:45:00′) = 45
B = Datepart(hh,’16:45:00′) = 16</pre>

So, we have
<pre>set @BeginTime = DateAdd(mi,-45-(16*60),’16:45:00′ ) = DateAdd(mi,-1005,’16:45:00′ )</pre>
And 1005 minutes are equal to 16 hours and 45 minutes, so the result is 0 minutes (i.e. ’00:00:00′).

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • LHalkape
    Thanks!!
    15 pointsBadges:
    report
  • carlosdl
    You are welcome. Summarizing, that expression will substract the hours and minutes from the original time, and the result will be just the seconds, which in this case were 0.
    77,040 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: