Solved

How to call this function

Posted on 2013-01-22
7
383 Views
Last Modified: 2013-01-22
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

Open in new window


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 )

Open in new window

0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 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

by:Guy Hengel [angelIII / a3]
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  

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38807468
Seems like knight is correct. Cross Apply one gives me repeated rows.
Anyway to exclude holidays?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 33

Expert Comment

by:knightEknight
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

by:Camillia
ID: 38807539
Should I create a Holiday table and join to that?
0
 
LVL 33

Expert Comment

by:knightEknight
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

by:knightEknight
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question