views:

48

answers:

2

Hello everyone,

I am using SQL Server 2008 Enterprise on Windows Server 2008 Enterprise. I have a question about tsql in SQL Server 2008. For select-where statement, there are two differnet forms,

(1) select where foo between [some value] and [some other value],

(2) select where foo >= [some value] and foo <= [some other value]? I am not sure whether between-and is always the same as using <= and >= sign?

BTW: whether they are always the same - even for differnet types of data (e.g. compare numeric value, comparing string values), appreciate if anyone could provide some documents to prove whether they are always the same, so that I can learn more from it.

thanks in advance, George

+4  A: 

This link has such a good explanation of your question.

How Does Between Work With Dates In SQL Server?

Muhammad Kashif Nadeem
How about for non-date types? Like numeric and string types?
George2
Martin Smith has explained it very good.
Muhammad Kashif Nadeem
Hi Muhammad, how to see execution plans esaily in SQL Server 2008 Management Studio? Sorry, I am new to this topic.
George2
In Management Studio; Go to menue Query -> Display Estimated Execution Plan. OR use shortcut ctrl+L
Muhammad Kashif Nadeem
Thanks, besides using query plan, do you have any document to prove they are the same?
George2
+5  A: 

Yes they are always the same. The entry in Books Online for BETWEEN says

BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.

Indeed you can see this easily by looking at the execution plans. You will see that Between doesn't even appear in the text. It has been replaced with >= and <= there is no distinction made between the two.

SELECT * FROM master.dbo.spt_values 
WHERE number between 1 and 3 /*Numeric*/

SELECT * FROM master.dbo.spt_values 
WHERE name between 'a' and 'b' /*String*/

select * from sys.objects 
WHERE create_date between GETDATE() and GETDATE()+100 /*Date*/

Plans

Martin Smith
How to see execution plans esaily in SQL Server 2008 Management Studio? Sorry, I am new to this topic.
George2
@Martin Smith, thanks for explaining this. I was just thinking that how can I explain this.
Muhammad Kashif Nadeem
@George2 - On the "Query" menu select "include Actual Execution Plan" then run the query or `CTRL+M` this shows the actual plan. Edit: Muhammad has explained how to see the estimated plan. The estimated plan can be better as you don't even have to execute the queries.
Martin Smith
Cool, thanks! :-)
George2
Thanks, besides using query plan, do you have any document to prove they are the same?
George2
Books online - See edit.
Martin Smith