0

I have an MS Access database with 40 similar tables. Only a couple are listed below for simplicity. I want to grab 3 cells of data from a row if the date in that row is yesterday. The data comes from some other employee's homebrew spreadsheets that they refuse to give up. So I can't change the column names or get them all on the same workbook template. I've tried some variations on the code below but I can't get the dates to work right. If I drop the WHERE statement it returns everything. But I really need it whittled down. Thanks in advance!

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1081
WHERE [Quote Completed] = date()-1
UNION

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1145
WHERE [Quote Completed] = date()-1
UNION

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1149
WHERE [Quote Completed] = date()-1
UNION 

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1228
WHERE [Quote Completed] = date()-1
UNION 

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1353
WHERE [Quote Completed] = date()-1
;
8
  • try using parantheses (date()-1)
    – DesirePRG
    Apr 14, 2015 at 19:06
  • what happens if you hard code the date value? will it return data? thinking it may be an issue with the syntax of the date function
    – Jared
    Apr 14, 2015 at 19:08
  • 1
    Is [Quote Completed] a date or a datetime? You might use WHERE cast([Quote Completed] as date) = date() -1 instead if the latter is true.
    – CactusCake
    Apr 14, 2015 at 19:12
  • 1
    What worked? You wrote that under both the question comments and @sqrfv's answer.
    – CactusCake
    Apr 14, 2015 at 20:22
  • 1
    It will be forever an internet mystery.
    – Noah
    Apr 15, 2015 at 14:21

3 Answers 3

1

Do you really need a where condition on each section of your union? Or do you just want the whole results filtered?

SELECT * FROM (
    SELECT [Customer name],[$ Amount], [Quote Completed]
    FROM 1081
    UNION
    SELECT [Customer name],[$ Amount], [Quote Completed]
    FROM 1145
    UNION
    SELECT [Customer name],[$ Amount], [Quote Completed]
    FROM 1149
    UNION 
    SELECT [Customer name],[$ Amount], [Quote Completed]
    FROM 1228
    UNION
    SELECT [Customer name],[$ Amount], [Quote Completed]
    FROM 1353) U
WHERE [Quote Completed] = date()-1
-1

You may need to use dateadd:

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1081
WHERE [Quote Completed] = DateAdd('d',-1, date())

Added link: http://www.techonthenet.com/access/functions/date/dateadd.php

2
  • It's still not showing me any results when I hit Run. This is only like my 4th database ever so I may be missing something stupid simple.
    – msiudut
    Apr 14, 2015 at 19:23
  • Access permits the ordinary addition and subtraction operators to be used to add or subtract numbers of days to/from date values. Apr 14, 2015 at 19:43
-2
SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1081
WHERE ([Quote Completed] = date()-1)
UNION

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1145
WHERE ([Quote Completed] = date()-1)
UNION

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1149
WHERE ([Quote Completed] = date()-1)
UNION 

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1228
WHERE ([Quote Completed] = date()-1)
UNION 

SELECT [Customer name],[$ Amount], [Quote Completed]
FROM 1353
WHERE ([Quote Completed] = date()-1)
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.