When reading data from the database with Between Operator, we can specify a range for the data we filter in WHERE Clause. Let’s make an example for a better understanding of the subject.
Example:
First, let’s create a table as below and add a few records.
1 2 3 4 5 6 7 8 9 | USE [TestDB] GO CREATE TABLE [dbo].[MyTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NOT NULL, [Age] int NOT NULL ) ON [PRIMARY] GO INSERT INTO [dbo].[MyTable] VALUES ('Nurullah',34),('Faruk',27),('Hakan',36),('Ogun',27) |
List the people aged 30 to 40 years old:
1 | SELECT * From [dbo].[MyTable] WHERE Age Between 30 AND 40 |
List the people whose names are between N and P:
1 | SELECT * From [dbo].[MyTable] WHERE Name Between 'N' AND 'P' |
List the people whose names are not between N and P:
1 | SELECT * From [dbo].[MyTable] WHERE Name NOT Between 'N' AND 'P' |