views:

6433

answers:

5

For instance can select foo from bar where foo between 5 and 10 select 5 and 10 or they are excluded from the range?

+9  A: 

The BETWEEN operator is inclusive.

Form BOL:

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.

DJ
+2  A: 

BETWEEN (Transact-SQL)

Specifies a(n) (inclusive) range to test.

test_expression [ NOT ] BETWEEN begin_expression AND end_expression

Arguments

test_expression

Is the expression to test for in the range defined by begin_expression and end_expression. test_expression must be the same data type as both begin_expression and end_expression.

NOT

Specifies that the result of the predicate be negated.

begin_expression

Is any valid expression. begin_expression must be the same data type as both test_expression and end_expression.

end_expression

Is any valid expression. end_expression must be the same data type as both test_expression and begin_expression.

AND

Acts as a placeholder that indicates test_expression should be within the range indicated by begin_expression and end_expression.

Remarks

To specify an exclusive range, use the greater than (>) and less than operators (<). If any input to the BETWEEN or NOT BETWEEN predicate is NULL, the result is UNKNOWN.

Result Value

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.

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

Russ Cam
+7  A: 

Yes, but be careful when using between for dates.

BETWEEN '01/01/2009' AND '01/31/2009'

is really interpreted as 12am, or

BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 00:00:00'

so will miss anything that occurred during the day of Jan 31st. In this case, you will have to use:

BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 23:59:59'

or

myDate >= '01/01/2009 00:00:00' AND myDate < '02/01/2009 00:00:00'
BradC
+1  A: 

Real world example from SQL Server 2008.

alt text

rodey
+1 thanks very much...
Abu Hamzah
A: 

depends on which version of sql server you are using..

Anil