Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

My WHERE Clause Using DatePart is Not Working the Way I Need it to

Posted on 2013-05-22
4
Medium Priority
?
473 Views
Last Modified: 2013-05-22
I am using the following WHERE clause to attemp to pull only data from LAST WEEK from a table

WHERE DatePart("ww", [15th_St_Panel_Plant_FPY.ProductionDate]) = (DatePart("ww", Date())-1) AND DatePart("yyyy", [15th_St_Panel_Plant_FPY.ProductionDate]) = (DatePart("yyyy", Date()));


However, when I compare the query return to manually calculated data from Last Week, the numbers do not match.

The ProductionDate is in the format mm/dd/yyyy. The first day of the week is intended to be Sunday.

Is there something in my Syntax, or am I just wrong in trying to do it this way?
0
Comment
Question by:Rex85
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 39187990
try:

WHERE [ProductionDate] >= DateAdd("d", -weekday(Date())-6, Date())
AND [ProductionDate] < DateAdd("d", -Weekday(Date()) + 1, date())
0
 

Author Closing Comment

by:Rex85
ID: 39188012
That worked...Thanks!
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 39188033
You may need to tweak the values of -6 and +1 to get the precise definition of "last week" that you are looking for.

Glad to help.
0
 

Author Comment

by:Rex85
ID: 39188453
Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question