Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Between Dates In Excel Macro

Author  Topic 

MechMon
Starting Member

2 Posts

Posted - 2011-06-10 : 08:51:32
Hello All,
I have a table with data like this:

Salesman Location SalesDate

What I would like to query is:

Select * from Salesman Where [SalesDate] Between '01/01/01' AND '01/30/01' OR [SalesDate] Between '06/01/11' AND '06/09/11'

I retrieve the first set of data fine ie,
Bob 01/01/01
Bob 01/02/01
Bob 01/15/01
Which is correct and complete.
However, the query then pulls in all Saleman from the "OR" date ie,
Bob 06/01/11
Steve 06/03/11
Sven 06/09/11
Bob 06/05/11
Sue 06/02/11
Bob 06/23/11
B'jorn 06/04/11


The resulting table looks like this:
Bob 01/01/01
Bob 01/02/01
Bob 01/15/01
Bob 06/01/11
Steve 06/03/11
Sven 06/09/11
Bob 06/05/11
Sue 06/02/11
Bob 06/23/11
B'jorn 06/04/11

It seems I need to match the "Where" with a "Between".
However this fails:
Select * from Salesman Where [SalesDate] Between '01/01/01' AND '01/30/01' OR Where [SalesDate] Between '06/01/11' AND '06/09/11'

If I run two different queries, the resulting table is over written.
Do I need to append the second set of data to the first? If so where do I specify it.

Regards,
MechMon

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-10 : 09:06:48
WHat do you actually want?
The first result with 10 rows looks like what you are asking for.

>> Which is correct and complete.
If that's thecase just remove the "or" part

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-06-12 : 04:22:04
whats the datatype of SalesDate?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

MechMon
Starting Member

2 Posts

Posted - 2011-06-15 : 07:40:33
[Solved]

I found the issue. Parenthesis are needed around each [SalesDate] and the entire expression as well, i.e.

(([SalesDate] Between '01/01/01' AND '01/30/01') OR ([SalesDate] Between '06/01/11' AND '06/09/11'))

Thank you.
Go to Top of Page
   

- Advertisement -