Question

please can you help me with my problem? So i have problem that i must search some data for some time but evertime return me diferent value.

// This return me 10k values
YEAR(pol.datum) >= YEAR(GETDATE()) - 1 and YEAR(pol.datum) <= YEAR(GETDATE())

// This return me 14k values 
YEAR(pol.datum) = YEAR(GETDATE()) and MONTH(pol.datum) <= MONTH(GETDATE()) - 1 and MONTH(pol.datum) >= MONTH(GETDATE()) - 7

// this return me 16k value
YEAR(pol.datum) = YEAR(GETDATE()) and MONTH(pol.datum) >= MONTH(GETDATE()) - 4 and MONTH(pol.datum) <= MONTH(GETDATE()) - 1

Can you tell me why it return me more value for some months than after all year

Was it helpful?

Solution

Posting this as an answer since there won't be enough room in the comments
You should post your entire query because the current statements should yield more results whereas for you they return less.

Play around with the following code to see for yourself :

declare @test Datetime;
select @test = '2013-1-04';

select 1
where YEAR(@test) > YEAR(GETDATE()) - 1 
and YEAR(@test) <= YEAR(GETDATE())

select 1
where YEAR(@test) = YEAR(GETDATE()) 
and MONTH(@test) <= MONTH(GETDATE()) - 1 
and MONTH(@test) >= MONTH(GETDATE()) - 7

select 1
where YEAR(@test) = YEAR(GETDATE()) 
and MONTH(@test) >= MONTH(GETDATE()) - 4 
and MONTH(@test) <= MONTH(GETDATE()) - 1

change to value of test and you will not find a value that triggers the second or third without triggering the first.
In conclusion : post your entire query :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top