Filter by dates

1

I need to be able to make a query that shows me the products that were not sold in a range of dates.

The Database:

The table:

select p.ProductID,p.ProductName,p.CategoryID,o.OrderDate,DAY(o.OrderDate) as 'Dia Vendido',MONTH(o.OrderDate) as 'Mes Vendido',YEAR(o.OrderDate) as 'Año Vendido'
from Products p join [Order Details] od on p.ProductID = od.ProductID
join Orders o on o.OrderID=od.OrderID
where p.Discontinued = 0 
and p.CategoryID = 8 
and YEAR(o.OrderDate)=1996 
and (o.OrderDate not between '1996-08-01' and '1996-08-15')

It's wrong, because it should come out like this:

    
asked by Adrian 09.06.2017 в 09:32
source

2 answers

1

I would try like this:

select p.ProductID,p.ProductName,p.CategoryID,o.OrderDate,DAY(o.OrderDate) as 'Dia Vendido',
MONTH(o.OrderDate) as 'Mes Vendido',YEAR(o.OrderDate) as 'Año Vendido'
from Products p join [Order Details] od on p.ProductID = od.ProductID
join Orders o on o.OrderID=od.OrderID
where p.Discontinued = 0 and p.CategoryID = 8 and 
o.OrderDate > '1996-01-01 00:00:00' and 
o.OrderDate < '1996-08-01 00:00:00' and 
o.OrderDate > '1996-08-15 0:00:00'

In any case it indicates what fault it gives you with your query. It will also show you more things than the ID, name and category, you have put more things in the select, if you only want to show you Id, name and category put:

select p.ProductID,p.ProductName,p.CategoryID from ...
    
answered by 09.06.2017 в 10:37
0

Use the BETWEEN

Something like NOT BETWEEN FECHA_INICIO AND FECHA_FIN should be enough.

Example:

Select * from persona where persona.fecha_nacimiento not between '2017-06-06' and '2017-06-08';
    
answered by 09.06.2017 в 09:39