Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Which is faster, BETWEEN or <= and >= ?

Author  Topic 

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-22 : 11:14:28
I'm guessing BETWEEN is faster, but wonder if anyone has done any tests to confirm it.

For example:

SELECT NAME
FROM PRSN
WHERE BIRTHDATE BETWEEN @DATE1 AND @DATE2

or...

SELECT NAME
FROM PRSN
WHERE BIRTHDATE >= @DATE1
AND BIRTHDATE <= @DATE2

Thanks!

SamC
White Water Yakist

3467 Posts

Posted - 2006-02-22 : 11:19:39
I'll bet there's no difference at all.

Look at the execution plans for verification.
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-22 : 11:45:06
Thanks Sam, they both performed the same based on the execution plans.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-22 : 13:07:06
I always fret a bit about what value is the limit at the top end, and usually I want to do:

WHERE BIRTHDATE >= @MyStartDate
AND BIRTHDATE < @MidnightFollowingMyEndDate

so BETWEEN doesn't suit me very well!!

Kristen
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-22 : 13:18:23
I'm confused, how is that different than <= ? My understanding was that BETWEEN actually includes the specified low and high end range values. Can you help me understand the situation that the < @MidnightFollowingMyEndDate accounts for?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-22 : 14:05:54
If I want everything from 01-Feb-2006 to 22-Feb-2006 then I need to include any values which have a date on 22-Feb-2006 AND anything with a TIME - up to midnight. So instead of

<= '22-Feb-2006' (this will only select upto, and including, midnight between 21st/22nd Feb)

I need to say

< '23-Feb-2006' (This will select up to, but not including, midnight 22nd/23rd Feb, which is what I want )

Kristen
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-22 : 14:13:48
Hey that makes sense! Thanks K.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-22 : 14:14:38
"Hey that makes sense!"

That's gotta be a first for me, ta!

Kristen
Go to Top of Page

Tazz602
Starting Member

11 Posts

Posted - 2006-02-22 : 15:13:53
In answer to the original question - the DB engine most likely interprets between as >= and <= (I know that Oracle does it that way) - so if you really want faster - the later would be your answer but probably not where you would notice it.
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-24 : 13:42:25
seems to be the case thanks Tazz.
Go to Top of Page
   

- Advertisement -