I have a problem, when filtering by dates, the field by which I want to filter has this format
This is the query I have
DECLARE @FechaInicio DATE;
DECLARE @FechaFin DATE;
SET @FechaInicio = '2021-01-01'
SET @FechaFin = '2021-09-30'
SELECT DISTINCT
c.FullnameLocal AS [Nombre Propietario de cuenta],
c.TaxCode AS [Identidad],
co.Description AS [Nombre Cuenta Politica],
ccr.ContractNumber AS [Numero de Cuenta],
co.Created AS [Fecha de Creacion de Cuenta],
FROM dbo.Clients c
INNER JOIN dbo.CustomerContractRelations ccr ON ccr.ClientId = c.ClientId AND ccr.CustomerContractRelationTypeId=1
INNER JOIN dbo.Contracts co ON co.ContractNumber = ccr.ContractNumber
WHERE co.Description LIKE '%POLITICA%LIMPIA%' OR
co.Description LIKE '%/%PARTIDO%' OR co.Description LIKE '%/%PARTIDO%'
OR co.Description LIKE '%PARTIDO%' OR co.Description LIKE '%POLITICA%'
AND co.ContractDate BETWEEN '2021-01-01' AND '2021-09-30'
As you can see I am filtering only from the year 2021, but in my result I am getting years prior to 2021 and I don't know why
The main problem is one of logic:
The
OR
overrides the date filter, any condition that is metDescription
will retrieve rows regardless of the date. The solution is to use the parentheses:Although it has nothing to do with your question, the other problem is the date filter when you have date + time, and you only use the date as a filter, the ideal would be