-1

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

user2813962
  • 3
  • 1
  • 1
  • 4
  • Could you elaborate a bit? What is this pol object? Are you writing a select statement? You speak of returning 10K values but all the lines return either true or false. – Kristof Oct 08 '13 at 07:54
  • all lines return true values after one year but why other settings for time return me more value than one year – user2813962 Oct 08 '13 at 07:56

1 Answers1

2

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 :)

Kristof
  • 3,267
  • 1
  • 20
  • 30
  • Well, this *could* be posted as a comment if you put the script at [SQL Fiddle](http://sqlfiddle.com/) and referred to it only in the form of a link. However, I believe answers of this kind (*‘the issue is elsewhere’*) are sometimes valid, and in particular I find your post a valid answer to this specific question. – Andriy M Oct 09 '13 at 05:45