Introduction:

  • In this article we will explain how to use SQL BETWEEN Operator in SQL Server OR what is SQL BETWEEN Operator in SQL Server with Example.

Description:

The BETWEEN operator in SQL Server are used to select values within a given range.  It is used in a WHERE clause in SELECT, UPDATE and DELETE statements queries .The BETWEEN operator is inclusive begin and end values are included. The values can be numbers, text, or dates.

  • BETWEEN Syntax
SELECT column1,column2,column3,columnN FROM table_name
WHERE column_name BETWEEN value1 AND value2;

 

  • Demo Database Table

Below is selected records from the " ProductOrder" table .

SQL BETWEEN Operator


  • BETWEEN Example

The following SQL statement selects all Orders with a price BETWEEN 1200 and 5000:

SELECT * FROM ProductOrder
WHERE Price BETWEEN 1200 AND 5000;

 

  • NOT BETWEEN Example

The following SQL statements display the Orders outside the range of the price BETWEEN 1200 and 5000:

SELECT * FROM ProductOrder
WHERE Price NOT BETWEEN 1200 AND 5000;   

 

  • BETWEEN with IN Example

The following SQL statement selects all Orders with a price BETWEEN 1200 and 5000. In addition; do not show products with a CategoryID of 1,2, or 3:

SELECT * FROM ProductOrder
WHERE (Price BETWEEN 1200 AND 5000) AND NOT CategoryID IN (1,2,3);


  • BETWEEN Dates Example

The following SQL statement selects all orders with an OrderDate BETWEEN '02- january-2017' and '30- april-2017':

SELECT * FROM ProductOrder
WHERE OrderDate BETWEEN '01/02/2017' AND '04/30/2017';

  • BETWEEN Text Values Example

The following SQL statement selects all Orders with a ProductName BETWEEN ‘TV' and 'Washing Machines':

SELECT * FROM ProductOrder
WHERE ProductName BETWEEN 'TV' AND 'Washing Machines'
ORDER BY ProductName;

  • NOT BETWEEN Text Values Example

The following SQL statement selects all Orders with a ProductName NOT BETWEEN ‘TV' and 'Washing Machines':

SELECT * FROM ProductOrder
WHERE ProductName NOT BETWEEN 'TV' AND 'Washing Machines'
ORDER BY ProductName;


I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like