Sunethpiumal
Sunethpiumal

Reputation: 641

SQL command for multiple "AND" & "OR" operation

I am going to write a sql command to retreive data by choosing multiple or selection. I have given written query by me, does anyone have alternative sql query for my query which gives same result?

It gives startdate for userid 4 and userid 37 in between given date range.

SELECT 
   UserID, StartDate, EndDate
FROM   
   dbo.TimeEntry
WHERE  
   (UserID = 4) AND (StartDate BETWEEN CONVERT(DATETIME, '2011-02-02 00:00:00',    
102) AND CONVERT(DATETIME, '2011-02-05 00:00:00', 102)) OR
(UserID = 37) AND (StartDate BETWEEN CONVERT(DATETIME, '2011-02-02 00:00:00', 102)
AND CONVERT(DATETIME, '2011-02-05 00:00:00', 102))  

Upvotes: 0

Views: 300

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135818

SELECT UserID, StartDate, EndDate 
    FROM dbo.TimeEntry 
    WHERE UserID IN (4,37) 
        AND StartDate BETWEEN CONVERT(DATETIME, '2011-02-02 00:00:00', 102)
                          AND CONVERT(DATETIME, '2011-02-05 00:00:00', 102)

Upvotes: 2

Related Questions