Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find date range overlap via SQL query

Status
Not open for further replies.

keijen

Programmer
Jul 12, 2001
16
AU
Hi,

Table: tblOnCall
Fields: PKey
DeptId
StartDate
EndDate


On insert or update of a new record, I need to count how many records have any part of the new record date range overlapping any part of a date range in the table, for the matching DeptId. There may be many records with the same DeptId.

i.e something like SELECT COUNT(*) FROM ONCALL WHERE (overlap stuff calculated)

How to??

All help appreciated.

TIA.
keijen
 
How about a function which takes the newStartDate and nesEndDate and uses a where clause:
Where newStartDate >= StartDate or newEndDate <= EndDate
 
Just noticed that the where clause missed some possibilities so we'll try again with:
Where newStartDate >= StartDate And nesStartDate <= EndDate)or (newEndDate <= EndDate and newEndDate >= StartDate) or (newStartDate <= StartDate and newEndDate >= EndDate)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top