• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Criteria last 2 days excluding weekends

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
repco
Asked:
repco
  • 4
  • 3
  • 2
  • +3
1 Solution
 
javaftperCommented:

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
 
p912sCommented:
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
 
JVWCCommented:
You might find this helpful.

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

Cheers
JC
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Gustav BrockCIOCommented:
Use the function below like this:

  Where [DateEntered] Between ISO_WorkdayAdd(Date(), -1) And ISO_WorkdayAdd(Date(), -2)

/gustav
Public Function ISO_WorkdayAdd( _
  ByVal datDate As Date, _
  ByVal lngWorkdaysAdd As Long, _
  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
' 2000-10-03. Constants added.
'             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
  
  On Error GoTo Err_ISO_WorkdayAdd
  
  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
      If lngWorkdaysAdd >= 0 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.
    If lngWorkdaysAdd >= 0 Then
      ' 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.
    datDate = DateAdd("d", -intDaysShift, datDate)
    ' Calculate workdays to add from start/end of current work week.
    lngWorkdaysAdd = lngWorkdaysAdd + intDaysShift
    
    ' Calculate number of workweeks and additional workdays to add.
    lngWeeks = lngWorkdaysAdd \ bytWorkdaysOfWeek
    lngDays = lngWorkdaysAdd Mod bytWorkdaysOfWeek
    
    ' Add number of calendar weeks and additional calendar days to
    ' shifted date datDate.
    If lngWeeks <> 0 Then
      datDate = DateAdd("ww", lngWeeks, datDate)
    End If
    If lngDays <> 0 Then
      datDate = DateAdd("d", lngDays, datDate)
    End If
  End If
  
  ISO_WorkdayAdd = datDate
  
Exit_ISO_WorkdayAdd:
  Exit Function
  
Err_ISO_WorkdayAdd:
  ' Date datDate + lngWorkdaysAdd is outside date range of Access.
  ' Return time zero, 00:00:00.
  Resume Exit_ISO_WorkdayAdd

End Function

Open in new window

0
 
Dale FyeCommented:
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
 
Dale FyeCommented:
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
 
Dale FyeCommented:
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
 
repcoAuthor Commented:
what i did was if weekday is MOnday then Date - 2, if Friday then  date. simple logic that i missed out
0
 
p912sCommented:
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
 
Gustav BrockCIOCommented:
Oh well - at least my solution works ...

/gustav
0
 
repcoAuthor Commented:
sorry i was away for 3 days, i give credit to the first person that answered the same day
0
 
Gustav BrockCIOCommented:
I think that the idea behind the site is that other people later can search for solutions to similar questions.

/gustav
0
 
Dale FyeCommented:
I'm not worried about the points, just want to make sure you have a workable solution.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now