Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHERE clause returning incorrect records

I'm using this

DECLARE @Year_Filter_Start AS DATETIME
SET @Year_Filter_Start = DATEADD( dd, -1, DATEADD( yy, DATEDIFF( yy, 0, GetDate() ), 0 ) )
DECLARE @Year_Filter_End AS DATETIME
SET @Year_Filter_End = GetDate()

INSERT INTO TABLE
  ( blah )
SELECT blah
  FROM OTHER_TABLE
 WHERE ACTISSUEDATE IS NULL 
    OR ACTSTARTDATE BETWEEN @Year_Filter_Start AND @Year_Filter_End

and it's returning records where ACTISSUEDATE is not null and ACTSTARTDATE is not between the year start and today. @Year_Filter_Start is supposed to be the beginning of this year, @Year_Filter_End is supposed to be today.

For example:

A record where ACTSTARTDATE is 2010-08-02 and ACTISSUEDATE is 2011-03-15

Or where ACTSTARTDATE is 2009-05-18 and ACTISSUEDATE is 2009-09-06

Is there something wrong with this statement?

like image 543
user971840 Avatar asked Nov 13 '22 04:11

user971840


1 Answers

Try the following, you need a few more parentheses I think:

WHERE ((ACTISSUEDATE IS NULL)  
    OR (ACTSTARTDATE BETWEEN @Year_Filter_Start AND @Year_Filter_End))

The engine sees this as two statements the way you have it. You need to make it one.

like image 196
virtualadrian Avatar answered Dec 24 '22 12:12

virtualadrian