SQL Server Query with Nulls and OR with dates

Im having a nightmare trying to write this SQL query

I have three fields

active
startdate
repeatdate

I want to exclude records where startdate is in this month
and exclude records were repeatdate is in this month
but include records where repeatdate is null
and include all records where active=1

tried for 2 hours, maybe im doing something wrong, but i cant get the AND and the ORs to work at all when testing for the NULL

tried recordset filter after the query , but it doesnt like it.

any ideas?
Nick_SDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
where active=1
   OR ( active=0
     AND  (
              ( startdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) ) )
              AND
              ( repeatdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) ) or repeatdate is null )
            )
    )

0
SharathData EngineerCommented:
try this.
SELECT * 
  FROM your_table 
 WHERE (DATEPART(MONTH,startdate) <> DATEPART(MONTH,GETDATE()) 
        AND (DATEPART(MONTH,repeatdate) <> DATEPART(MONTH,GETDATE()) 
              OR repeatdate IS NULL)) 
        OR active = 1

Open in new window

0
gladfellowCommented:
Both answers mentioned above (kinghtEknight and Sharath123) are correct based on one particular understanding of the need.

However, the question sounded ambiguous to me and I understood your requirement slightly differently as follows:
1. Include only rows where active = 1
2. Within the above subset, if repeatdate is NULL, include those rows; otherwise, if repeatdate is not NULL, then include those rows only if both their startdate and repeatdate fall outside the current month

With this understanding, I have included below a file containing sample data, suggested SQL and result rows. Check out and post your feedback as to what was your actual requirement and whether any of the above answers have been helpful.

 A-date-with-NULLs.txt
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Nick_SDAuthor Commented:
mmmm , so what happens to records from 1/4/2010 , they will be excluded aswell wont they ?

gladfellow, i dont this your example worked as it would still bring results for the current month
for startdate or repeatdate, your results show this. (results line 3)
0
Alpesh PatelAssistant ConsultantCommented:
Select * From Table
where month(startdate) = month(getdate())
and year(startdate) = year(getdate())
and month(repeatdate) = month(getdate())
and year(repeatdate) = year(getdate())
and active =1
and repeatdate is null
0
Alpesh PatelAssistant ConsultantCommented:
Sorry Ignore above one



Select * From Table
where month(startdate) <> month(getdate())
and year(startdate) = year(getdate())
and month(repeatdate) <> month(getdate())
and year(repeatdate) = year(getdate())
and active =1
and repeatdate is null
0
Nick_SDAuthor Commented:
Alpesh , the date cannot be NULL and less than current month. There has to be an OR in there surely
0
knightEknightCommented:
> "exclude records where startdate is in this month"

I interpreted this to mean the current month and year -- is this correct?  Does my post satisfy all the requirements?
0
gladfellowCommented:
It would be helpful, if you post the exact result set that you expect, based on some sample data. Thanks.

- Venkat
0
Nick_SDAuthor Commented:
active startdate repeatdate
1       1/4/2011  NULL
1        1/3/2011  NULL
1        1/3/2011  1/4/2011
1        1/4/2010   15/3/2011
1        1/3/2010   1/4/2011
0         1/2/2011   1/3/2011


I want to receive the following results
1        1/3/2011  NULL
1        1/4/2010   15/3/2011

based on the fact that the startdate nor the repeatdate are no in this month. and active =1
hope that makes sense.



0
knightEknightCommented:
so you don't want any rows where active=0 then?
0
Nick_SDAuthor Commented:
yes active must equal 1
0
knightEknightCommented:
okay ... originally you said:  "and include all records where active=1"  so I assumed that all the other criteria must apply only to rows where active = 0.

Also, what is the date format you are using in the expected results above?  is it mm/dd/yyyy or dd/mm/yyyy ?


 where active=1
  AND  (
             ( startdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) ) )
             AND
             ( repeatdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) ) or repeatdate is null )
         )

0
Nick_SDAuthor Commented:
thanks for that

date format is dd/mm/yyyy

the code you wrote brings back 11/4/2011 as included.

I want to exclude records where startdate is in this month
and exclude records were repeatdate is in this month
0
knightEknightCommented:
is  11/4/2011  a startdate or a repeatdate?
0
knightEknightCommented:
also, what is the result of this query on your system?

select convert( datetime, -datediff(day,getdate(),day(getdate())-1) ) as day1
0
Nick_SDAuthor Commented:
sorry repeatdate
0
knightEknightCommented:
Let's focus on the startdate first ... does the following query work with respect to your startdate criteria?

  WHERE active=1
     AND  startdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) )

If so then we can add in the criteria for repeatdate later ...
0
Nick_SDAuthor Commented:
yes this works, i just tested the same SQL , and it appears to exclude all aprils startdates
0
knightEknightCommented:
hmm, i can't see the problem with repeatdate then.  Here's something else to try:


  WHERE active=1
     AND  startdate < convert( datetime, -datediff(day,getdate(),day(getdate())-1) )
     AND  isNull(repeatdate,startdate) < convert( datetime, -datediff(day,getdate(),day(getdate())-1) )

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nick_SDAuthor Commented:
now mr knight, thats a winner , thank you very much , what a nightmare.

cheers
0
Nick_SDAuthor Commented:
brilliant
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.