Jump to content
  • 0
Sign in to follow this  
mmorales

Calculating Difference In Dates

Question

In a table that I have, I have a date field that indicates the date an order was placed. I would like to create a calculated field in my datapage that will indicate how long it has been since the order was open (that is, the difference between today's date and the order creation date). Is there any way to do this? I know it will involve Datediff but I am unsure of how to insert today's date automatically.

 

Ex.

Order Created - 1/20/16

Today's Date - 2/12/16

 

The expected output would be 23. 

 

Thank you.

 

EDIT: So I've implemented this using the following formula: DATEDIFF(day,OrderDate, GETUTCDATE()) and it works correctly but now I need this to ONLY count business days (at the moment, it is counting weekends as well).

 

EDIT 2: I've figured it out. I will leave the answer here for posterity:

 

To calculate the difference in dates (only considering business days), enter the following into your calculated field:

 

select (DATEDIFF(dd,@StartDate, @EndDate))    
- (DATEDIFF(ww,@StartDate, DATEADD(dd,-1,@EndDate)) * 2) 
- (CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 else 0 end)
- (CASE WHEN DATENAME(dw, @EndDate) = 'Sunday' THEN 1 else 0 end)
 
where you replace @StartDate and @EndDate with your corresponding fields (if you're calculating days from today, then use GETUTCDATE() for @EndDate).

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...