Solved

# Criteria last 2 days excluding weekends

Posted on 2010-08-16
406 Views
Hi,

how can i show only records where [DateEntered] is less than 2 days but excluding weekends?
So today (Monday), will only show records for Thurs and Fri.
0
Question by:repco
• 4
• 3
• 2
• +3

LVL 4

Accepted Solution

javaftper earned 500 total points
ID: 33447228

In Access SQL query as long as DateEntered is a date/time field (i.e. not a text field), you can use Format() to test whether the system date (Date() function) returns a Monday or not. If it does, -3 days, otherwise -1 day from Date().  So use a HAVING clause in your SELECT, as follows-

HAVING ((QUERY_TEMP.DateEntered)=IIf(Format(Date(),"ddd")="Mon",Date()-3,Date()-1))

0

LVL 18

Expert Comment

ID: 33448255
The following SQL will return the previous two days based on the day of the week; Monday = 2.

SELECT table2.mydate
FROM table2
WHERE (((table2.mydate) Between Date()-4 And Date()-3) AND ((Weekday(Date()))=2)) OR (((table2.mydate) Between Date()-2 And Date()-1) AND ((Weekday(Date()))>2 And (Weekday(Date()))<7));

Replace table2 and mydate with your table and date field names.

Hope that helps.
0

LVL 6

Expert Comment

ID: 33452515

http://support.microsoft.com/kb/210604/

Cheers
JC
0

LVL 49

Expert Comment

ID: 33453436
Use the function below like this:

/gustav
``````Public Function ISO_WorkdayAdd( _

ByVal datDate As Date, _

Optional ByVal bytWorkdaysOfWeek As Byte = 5) _

As Date

' Purpose: Add number of working days to date datDate.

' Assumes: 1 to 7 working days per week.

'          First workday is Monday.

'          Weekend is up to and including Sunday.

' Limitation: Does not count for public holidays.

' May be freely used and distributed.

' 1999-04-23. Gustav Brock, Cactus Data ApS, Copenhagen

'             Option for 5 or 6 working days per week added.

' 2002-01-10. Option for 1 to 7 working days per week added.

'             Allowed to add negative number of working days.

'             Adding of zero working days returns the next

'             working day if current day is not a working day.

' Test:

' For j = 0 To 10 : For i = 0 to 12 : ? j, i, ISO_WorkdayAdd(Date + j, i): Next i: Next j

' Minimum and maximum count of workdays per week.

Const cbytWorkdaysCountMin  As Byte = 1

Const cbytWorkdaysCountMax  As Byte = 7

Dim bytMonday               As Byte

Dim bytSunday               As Byte

Dim intWeekdayFirst         As Integer

Dim intWorkdayLast          As Integer

Dim intDaysShift            As Integer

Dim lngDays                 As Long

Dim lngWeeks                As Long

If bytWorkdaysOfWeek >= cbytWorkdaysCountMin And bytWorkdaysOfWeek <= cbytWorkdaysCountMax Then

' Find ISO weekday for Monday.

bytMonday = WeekDay(vbMonday, vbMonday)

' Find ISO weekday for Sunday.

bytSunday = WeekDay(vbSunday, vbMonday)

' Find ISO weekday for last workday.

intWorkdayLast = bytMonday + bytWorkdaysOfWeek - 1

' Find ISO weekday for date datDate.

intWeekdayFirst = WeekDay(datDate, vbMonday)

' Shift date datDate from weekend to Monday.

If intWeekdayFirst > intWorkdayLast Then

datDate = DateAdd("d", bytSunday - intWeekdayFirst + 1, datDate)

Else

datDate = DateAdd("d", intWorkdayLast - intWeekdayFirst, datDate)

End If

' Find ISO weekday for shifted date datDate.

intWeekdayFirst = WeekDay(datDate, vbMonday)

End If

' Calculate number of days date datDate shall be shifted.

' Shift to proceeding Monday in current week.

intDaysShift = intWeekdayFirst - bytMonday

Else

' Shift to succeeding last workday in current week.

intDaysShift = intWeekdayFirst - intWorkdayLast

End If

' Shift date datDate.

' Calculate workdays to add from start/end of current work week.

' shifted date datDate.

If lngWeeks <> 0 Then

End If

If lngDays <> 0 Then

End If

End If

Exit Function

' Date datDate + lngWorkdaysAdd is outside date range of Access.

' Return time zero, 00:00:00.

End Function
``````
0

LVL 47

Expert Comment

ID: 33453660
This query will not be updateable because of the subquery.  The subquery identifies the most recent two dates (prior to today's date) in your table which fall on a weekday.  This is then joined to your table on those dates to return all of the records with a [DateEntered] associated with those two dates.

SELECT yourTable.*
FROM yourTable
INNER JOIN
(SELECT TOP 2 yourTable.[DateEntered] from yourTable
WHERE yourTable.[DateEntered]<Date() AND Weekday(yourTable.[DateEntered],2)<6
ORDER BY yourTable.[DateEntered]) as temp
ON yourTable.[DateEntered] = temp.[DateEntered]

The weekday function returns a numeric value for the day of the week.  The second parameter (firstdayofweek) allows you to shift the "first day of the week" to any day, so if you use 2 (vbMonday), then weekdays will be identified as between 1 and 5.
0

LVL 47

Expert Comment

ID: 33453725
Oops, forgot to sort the subquery descending, so that the most recent dates would be at the top.  That should read:

SELECT yourTable.*
FROM yourTable
INNER JOIN
(SELECT TOP 2 yourTable.[DateEntered] from yourTable
WHERE yourTable.[DateEntered]<Date() AND Weekday(yourTable.[DateEntered],2)<6
ORDER BY yourTable.[DateEntered] DESC) as temp
ON yourTable.[DateEntered] = temp.[DateEntered]
0

LVL 47

Expert Comment

ID: 33458675
did you actually try the accepted solution?

The Having clause provided by javaftper is only good with an aggregate query, and will only get you one days worth of information, not two.

The option presented by p912s will work if the current day is Monday, Wed, Thur or Fri, but not on Tuesday.

The only two options that will really return all of the data from your table for the most recent two weekdays prior to the current date are the ones presented by cactus_data and myself.
0

Author Comment

ID: 33458707
what i did was if weekday is MOnday then Date - 2, if Friday then  date. simple logic that i missed out
0

LVL 18

Expert Comment

ID: 33458739
fyed - I was just wondering the same thing?

And when I was reviewing came to the same conclusion you did about my code failing on Tuesday...

0

LVL 49

Expert Comment

ID: 33458771
Oh well - at least my solution works ...

/gustav
0

Author Comment

ID: 33458811
sorry i was away for 3 days, i give credit to the first person that answered the same day
0

LVL 49

Expert Comment

ID: 33458962
I think that the idea behind the site is that other people later can search for solutions to similar questions.

/gustav
0

LVL 47

Expert Comment

ID: 33460313
I'm not worried about the points, just want to make sure you have a workable solution.
0