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 2008 Forums
 Transact-SQL (2008)
 Silly question about the BETWEEN operator

Author  Topic 

Abu-Dina
Posting Yak Master

206 Posts

Posted - 2012-08-02 : 12:13:26
Hi,

Exceuse the silly question but consider this:


create table #test(myID int, myText varchar(10))

insert into #test(myID, myText)
select 1, 'Red' union all
select 2, 'Yellow' union all
select 3, 'Blue' union all
select 4, 'Purple'

select * from #test
where myid between 1 and 3



works fine but if I change the between to
between 3 and 1


then nothing is returned.

Why?!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-02 : 12:33:03
BETWEEN is position sensitive. myid BETWEEN 1 and 3 translates to "myid >= 1 and myid <= 3". And, so BETWEEN 3 and 1 translates to "myid >= 3 and myid <= 1", which of course is empty
Go to Top of Page

Abu-Dina
Posting Yak Master

206 Posts

Posted - 2012-08-03 : 04:21:27
Thanks I understand now.
Go to Top of Page
   

- Advertisement -