tags:

views:

87

answers:

2

I have a table with a TestNumber column with four possibilites (ex: 1 - 4), a TestDate column, and other data. I want to make a query that will return a user defined year and test numbers. For example, I want to return test Numbers 1 and 2 that have a date with year 2008.

Edit: I basically want to combine these two statements into one statement:

SELECT * FROM testTable WHERE testNumber = '1' AND '2'
SELECT * FROM testTable WHERE testDate between #01/01/2008# and #01/01/2009#
+3  A: 

Try this:

SELECT * 
FROM testTable 
WHERE testNumber IN ('1', '2') 
    AND testDate between #01/01/2008# and #01/01/2009#
Manu
I'm currently using access
ScottK
@ScottK: it's crucial to specify the database engine when asking SQL questions. Otherwise you get perfectly well-meaning but not useful answers (like the one use Cast()).
David-W-Fenton
I think this answer is wrong because it doesn't return the same results as the UNION of the original question's 2 SQL statements. The AND should be changed to OR, no?
David-W-Fenton
I should have mentioned the db engine, but, I did want to see possible SQL Server answers too as I might be using that in the future for this application. I wish those answers didn't get deleted (the one using cast()).
ScottK
+3  A: 

I'm not sure anyone has answered this correctly yet. @Manu combines the criteria, instead of the result sets, so he won't get the UNION of the questioners two SQL statements. It seems to me, it should be:

  SELECT * FROM testTable WHERE testNumber In ('1', '2') 
     OR testDate between #01/01/2008# and #12/31/2008#

Note that some database engines (including Jet/ACE) interpret the values of a BETWEEN/AND pair inclusively, so the criteria ought to be the first and last dates of the year, rather than Jan. 1st of the two years.

But that will actually depend on the particular db engine's interpretation of BETWEEN/AND.

Note also that if the date fields have time parts, you'll need to use:

  testDate >= #01/01/2008# and testDate < #01/01/2009#

...instead of BETWEEN/AND.

David-W-Fenton
Manu's answer is what worked for me. I have a form where the user chooses which testNumbers they want (via check boxes) and which year they want (via combo box). I use those for the query.
ScottK
Thanks for the info about BETWEEN/AND concerning dates! I was not sure if it was inclusive or not.
ScottK
If @Manu's answer gave you correct results, you clearly mis-stated the result set that you needed. You said you needed the combination of two resultsets, which requires an OR. Maybe you should alter your question to reflect the fact that you *didn't* want the UNION of the two select statements you posted, but a single statement that combines both criteria.
David-W-Fenton
I see what you are saying. I wanted to combine the two query statements, not the result-set of each query.
ScottK