In this post explains how to use the BETWEEN condition in SQL Server (Transact-SQL) with syntax and examples.
The SQL Server (Transact-SQL) BETWEEN condition is used to retrieve values within a range in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the BETWEEN condition in SQL Server (Transact-SQL) is:
A column or calculation.
These values create an inclusive range that expression is compared to.
Let's look at some SQL Server BETWEEN condition examples using numeric values. The following numeric example uses the BETWEEN condition to retrieve values within a numeric range.
For example:
This SQL Server BETWEEN example would return all rows from the employees table where the employee_id is between 25 and 100 (inclusive). It is equivalent to the following SELECT statement:
Next, let's look at how you would use the SQL Server BETWEEN condition with Dates. The following date example uses the BETWEEN condition to retrieve values within a date range.
For example:
This SQL Server BETWEEN condition example would return all records from the employees table where the start_date is between May 1, 2014 and May 31, 2014 (inclusive). It would be equivalent to the following SELECT statement:
The SQL Server BETWEEN condition can also be combined with the SQL Server NOT operator. Here is an example of how you would combine the BETWEEN condition with the NOT Operator.
For example:
This SQL Server BETWEEN example would return all rows from the employees table where the employee_id was NOT between 2000 and 2999, inclusive. It would be equivalent to the following SELECT statement: