ajetrumpet
Banned
- Local time
- Yesterday, 21:02
- Joined
- Jun 22, 2007
- Messages
- 5,638
With the following SQL statements, you can query previous time periods within your database (given the fact that you have a date field(s) in the table(s) that you are querying.
Current Week to Date
Current Month to Date
Current Year to Date
Previous Months
Previous Weeks (using "DAYS" parameter)
Previous Weeks (using "WEEKS" parameter)
Previous Years
Current Week to Date
Code:
SELECT Table.DateField
FROM Table
WHERE [Table.DateField] BETWEEN
DateAdd("d", -((Weekday(Date()) - 1)), Date()) AND
Date();
Current Month to Date
Code:
SELECT Table.DateField
FROM Table
WHERE (((Table.DateField) BETWEEN
DatePart("m",Date()) & "/1/" & DatePart("yyyy",Date()) AND
Date()));
Current Year to Date
Code:
SELECT Table.DateField
FROM Table
WHERE (((Table.DateField) BETWEEN
"1/1/" & DatePart("yyyy",Date()) AND
Date()));
Previous Months
Code:
PARAMETERS [Number of Previous Months to Query] Short;
SELECT Table.DateField
FROM Table
WHERE [Table.DateField] BETWEEN
DateAdd("m", -[Number of Previous Months to Query],
DatePart("m", Date()) & "/1/" & DatePart("yyyy", Date())) AND
DateAdd("d", -1,
DatePart("m", Date()) & "/1/" & DatePart("yyyy", Date()));
Previous Weeks (using "DAYS" parameter)
Code:
PARAMETERS [Number of Previous Weeks to Query] Short;
SELECT Table.DateField
FROM Table
WHERE [Table.DateField] BETWEEN
DateAdd("d", (-[Number of Previous Weeks to Query] * 7),
DateAdd("d", -((Weekday(Date()) - 1)), Date())) AND
DateAdd("d", (([Number of Previous Weeks to Query] * 7) - 1),
DateAdd("d", (-[Number of Previous Weeks to Query] * 7),
DateAdd("d", -((Weekday(Date()) - 1)), Date())));
Previous Weeks (using "WEEKS" parameter)
Code:
PARAMETERS [Number of Previous Weeks to Query] Short;
SELECT Table.DateField
FROM Table
WHERE [Table.DateField] BETWEEN
DateAdd("ww", -[Number of Previous Weeks to Query],
DateAdd("d", -((Weekday(Date()) - 1)), Date())) AND
DateAdd("ww", [Number of Previous Weeks to Query],
DateAdd("ww", -[Number of Previous Weeks to Query],
DateAdd("d", -(Weekday(Date())), Date())));
Previous Years
Code:
PARAMETERS [Number of Previous Years to Query] Short;
SELECT Table.DateField
FROM Table
WHERE [Table.DateField] BETWEEN
DateAdd("yyyy", -[Number of Previous Years to Query],
"1/1/" & DatePart("yyyy", Date())) AND
"12/31/" & DatePart("yyyy", DateAdd("yyyy", -1, Date()));