Maybe I am missing something. I am trying to do a pretty simple select statement. The logic I want is that if two columns (ActivityDate and CreateDate) equal eachother AND ONE OR THE OTHER ours column and the DoubleBookedHours column are zero it should be returned:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND (
Hours != 0
AND DoubleBookedHours != 0
)
The above is returning nothing.
The first row in the above picture is what it SHOULD return, because not both of the columns Hours and DoubleBookedHours is zero but the dates are equivelant. The second row is what I don't want it to return because both Hours and DoubleBookedHours are zero.
What am I doing wrong?
Edit:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate AND Hours != 0 AND DoubleBookedHours != 0
The above Returns nothing.
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0
The above returns the opposite of what I want:
You were performing just the opposite. By using !
you are saying anything but 0. Also, there is no need to use the parenthesis in your where clause.
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0
IF you want those returned that don't have both Hours = 0
and DubleBookedHours = 0
, then:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND NOT
(Hours = 0 AND DoubleBookedHours = 0)
It should be:
--test data
DECLARE @DailyTaskHours AS TABLE (ActivityDate datetime, CreateDate datetime, [Hours] int, DoubleBookedHours int)
INSERT INTO @DailyTaskHours VALUES ('01 Jan 2012','01 Jan 2012',0,1)
INSERT INTO @DailyTaskHours VALUES ('01 Jan 2012','01 Jan 2012',0,0)
--query
SELECT *
FROM @DailyTaskHours
WHERE ActivityDate = CreateDate
AND ([Hours] <> 0 OR DoubleBookedHours <> 0)
--result
ActivityDate CreateDate Hours DoubleBookedHours
2012-01-01 00:00:00.000 2012-01-01 00:00:00.000 0 1
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND (
Hours != 0
OR -- this
DoubleBookedHours != 0
)
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.