Nick_SD
asked on
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?
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?
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
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
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
ASKER
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)
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)
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
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
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
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
ASKER
Alpesh , the date cannot be NULL and less than current month. There has to be an OR in there surely
> "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?
I interpreted this to mean the current month and year -- is this correct? Does my post satisfy all the requirements?
It would be helpful, if you post the exact result set that you expect, based on some sample data. Thanks.
- Venkat
- Venkat
ASKER
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.
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.
so you don't want any rows where active=0 then?
ASKER
yes active must equal 1
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(),da y(getdate( ))-1) ) )
AND
( repeatdate < convert( datetime, -datediff(day,getdate(),da y(getdate( ))-1) ) or repeatdate is null )
)
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(),da
AND
( repeatdate < convert( datetime, -datediff(day,getdate(),da
)
ASKER
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
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
is 11/4/2011 a startdate or a repeatdate?
also, what is the result of this query on your system?
select convert( datetime, -datediff(day,getdate(),da y(getdate( ))-1) ) as day1
select convert( datetime, -datediff(day,getdate(),da
ASKER
sorry repeatdate
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(),da y(getdate( ))-1) )
If so then we can add in the criteria for repeatdate later ...
WHERE active=1
AND startdate < convert( datetime, -datediff(day,getdate(),da
If so then we can add in the criteria for repeatdate later ...
ASKER
yes this works, i just tested the same SQL , and it appears to exclude all aprils startdates
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
now mr knight, thats a winner , thank you very much , what a nightmare.
cheers
cheers
ASKER
brilliant
OR ( active=0
AND (
( startdate < convert( datetime, -datediff(day,getdate(),da
AND
( repeatdate < convert( datetime, -datediff(day,getdate(),da
)
)