Ben said:
Can anyone tell me how I can compare date in MS Access?
here's what I trying to do..
+--------------+-------------|
| Start Date | End Date |
| 22/7/2005 | 7/9/2005 |
| 28/2/2005 | 28/4/2005 |
| 1/4/2005 | 30/6/2005 |
| ... | ... |
Critera: month of April exist between [Start Date] & [End Date]
output...
+--------------+-------------|
| Start Date | End Date |
| 28/2/2005 | 28/4/2005 |
| 1/4/2005 | 30/6/2005 |
|----------------------------- |
Thank you so much.. ^^
Public Function AprilDateIntersection(dt3 As Date, dt4 As Date) As Integer
Dim intSumDateIntersection As Integer
Dim dt1 As Date
Dim dt2 As Date
Dim dtTemp As Date
'Return the number of days of any April overlapping a date range
'Assumes d3 <= d4
intSumDateIntersection = 0
'Loop for all Aprils between years of dt3 and dt4
dtTemp = dt3
Do While Year(dtTemp) <= Year(dt4)
dt1 = DateSerial(Year(dtTemp), 4, 1)
dt2 = DateSerial(Year(dtTemp), 4, 30)
If dt2 <= dt3 Then
If dt2 = dt3 Then intSumDateIntersection = intSumDateIntersection + 1
ElseIf dt4 <= dt1 Then
If dt4 = dt1 Then intSumDateIntersection = intSumDateIntersection + 1
ElseIf dt1 <= dt3 And dt3 <= dt2 And dt2 <= dt4 Then
intSumDateIntersection = intSumDateIntersection + DateDiff("d",
dt3, dt2) + 1
ElseIf dt1 <= dt3 And dt3 <= dt4 And dt4 <= dt2 Then
intSumDateIntersection = intSumDateIntersection + DateDiff("d",
dt3, dt4) + 1
ElseIf dt3 <= dt1 And dt1 <= dt2 And dt2 <= dt4 Then
intSumDateIntersection = intSumDateIntersection + DateDiff("d",
dt1, dt2) + 1
ElseIf dt3 <= dt1 And dt1 <= dt4 And dt4 <= dt2 Then
intSumDateIntersection = intSumDateIntersection + DateDiff("d",
dt1, dt4) + 1
End If
dtTemp = DateAdd("yyyy", 1, dtTemp)
Loop
AprilDateIntersection = intSumDateIntersection
End Function
I only tried one date range (with U.S. date format) so let me know your
test results if you try it.
MsgBox (AprilDateIntersection(#3/1/2006#, #4/3/2006#)) => 3
For your situation try:
WHERE AprilDateIntersection([Start Date], [End Date]) > 0
Post back if you need to know how many complete Aprils are in the date
range.
James A. Fortune
(e-mail address removed)