Adrian
Adrian

Reputation: 3438

Selecting records with multiple between clauses

I am having trouble getting a sql STATEMENT to work.

I want to select records that are BETWEEN date 1 and date 2 and also

records that are BETWEEN date 3 and date 4.

This query is yielding no results.

(the values in single quotes are just pseudocode)

SELECT  *
FROM    dbo.Items
WHERE   ( ( PostDate BETWEEN Date1 AND Date2 )
          AND ( PostDate BETWEEN Date3 AND Date4 )
        )

Reading back this query it sounds like im asking the db to give me

records that are between date 1 and date 2 but also in between date 3 and date 4,

which does not make any sense.

How would I translate *Give me records that are between date 1 and date 2 and

records that are between date 3 and date 4 in one batch

Upvotes: 0

Views: 49

Answers (2)

Akshay
Akshay

Reputation: 1901

Your 'AND' condition is wrong in this case. Use 'OR' condition instead

SELECT * FROM dbo.Items WHERE ((PostDate BETWEEN Date1 AND Date2) OR (PostDate BETWEEN Date3 AND Date4))

Upvotes: 2

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

try to use union and divide this query into 2 queries.

SELECT * FROM dbo.Items WHERE PostDate BETWEEN Date1 AND Date2 
union
SELECT * FROM dbo.Items WHERE PostDate BETWEEN Date3 AND Date4 

Upvotes: 0

Related Questions