Go Premium for a chance to win a PS4. Enter to Win

x
Solved

How to call this function

Posted on 2013-01-22
Medium Priority
390 Views
I want to calculate 72 hours elapsed time but exclude the weekends. I found this function

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519&SearchTerms=F_TABLE_DATE.

I can call it directly like this:
`````` SELECT * FROM   F_TABLE_DATE('2012-12-07 10:55:25.220', GETDATE()) d
WHERE
d.[DAY_OF_WEEK] BETWEEN 2 AND 6
``````

But not sure how to call it. This is my sql but doesnt work

``````SELECT *
FROM dbo.NonMemberProvider
WHERE officecalled IS NULL
AND DATEDIFF(hour, UPDATEDate, GETDATE()) > 72
AND  ( SELECT * FROM F_TABLE_DATE(UPDATEDate, GETDATE())
WHERE
d.[DAY_OF_WEEK] BETWEEN 2 AND 6 )
``````
0
Question by:Camillia
• 4
• 2

LVL 33

Accepted Solution

knightEknight earned 2000 total points
ID: 38807422
-- here's something to try along the same line of thinking:

SELECT *
FROM dbo.NonMemberProvider
WHERE officecalled IS NULL
AND DATEDIFF(hour, UPDATEDate, GETDATE()) > 72
AND  DATEPART( WEEKDAY, UPDATEDate ) between 2 and 6
0

LVL 143

Expert Comment

ID: 38807424
it think you want to "CROSS APPLY" that table function:
``````SELECT t.*
FROM dbo.NonMemberProvider t
CROSS APPLY F_TABLE_DATE(t.UPDATEDate, GETDATE())  f
WHERE t.officecalled IS NULL
AND DATEDIFF(hour, t.UPDATEDate, GETDATE()) > 72
AND f.[DAY_OF_WEEK] BETWEEN 2 AND 6
``````
0

LVL 7

Author Comment

ID: 38807468
Seems like knight is correct. Cross Apply one gives me repeated rows.
Anyway to exclude holidays?
0

LVL 33

Expert Comment

ID: 38807517
Not unless you have a specific holiday or set of holidays in mind.  Certain holidays fall on different dates each year (for example, first Monday of the month), whereas others are always on the same day, in which case:

SELECT *
FROM dbo.NonMemberProvider
WHERE officecalled IS NULL
AND DATEDIFF(hour, UPDATEDate, GETDATE()) > 72
AND  DATEPART( WEEKDAY, UPDATEDate ) between 2 and 6
AND NOT ( DATEPART(month,UPDATEDate)=12 and DATEPART(day,UPDATEDate)=25 )  -- excludes December 25
0

LVL 7

Author Comment

ID: 38807539
Should I create a Holiday table and join to that?
0

LVL 33

Expert Comment

ID: 38807556
Yes, that could work... then you could excludes dates in that table:

SELECT *
FROM dbo.NonMemberProvider T
WHERE officecalled IS NULL
AND DATEDIFF(hour, UPDATEDate, GETDATE()) > 72
AND  DATEPART( WEEKDAY, UPDATEDate ) between 2 and 6
and UPDATEDate not in ( select holdate from HolidayTable )
0

LVL 33

Expert Comment

ID: 38807562
actually, my last post assumes UPDATEDate is a date type(as opposed to a datetime).  If it is a datetime then you could cast it like this, but doing this will slow down your query considerably:

and cast(UPDATEDate as DATE) not in ( select holdate from HolidayTable )
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based developeâ€¦
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server.Â RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at: Â  htâ€¦
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signalâ€¦
Suggested Courses
Course of the Month10 days, 16 hours left to enroll