SQL Between Operator For Dates, Strings Example

  Total Views : 1025
  Comments : 0
 
  Keywords : SQL between operator with example, Use of sql between operator with example, SQL between operator for string and datetime examples, Use sql between operator with not keyword example

Here we will learn between operator in sql with example, uses of between operator and how to use sql between operator with characters, date and not keywords with examples.

SQL BETWEEN Operator

The BETWEEN operator in SQL is used to get values within range. Generally we will use this BETWEEN operator in WHERE clause to get values within range. Syntax for BETWEEN operator in SQL will be like as shown below 

Syntax for SQL BETWEEN Operator

Following sql statement is the syntax of between operator in sql server

 

SELECT column1, column2 FROM tablename WHERE column1 BETWEEN value1 AND value2

If you observe above SQL BETWEEN operator syntax we are getting values where column1 value between value1 and value2. We will check this between operator with example for that create “EmployeeDetails” table by using following script in your database

 

create table EmployeeDetails(empid int, empname varchar(50),designation varchar(50),salary int,Location varchar(50))

 

insert into EmployeeDetails

values(1,'suresh','software engineer',25000,'chennai'),

(2,'rohini','AEO',15000,'chennai'),

(3,'madhavsai','business analyst',50000,'nagpur'),

(4,'mahendra','CA',75000,'guntur'),

(5,'sateesh','Doctor',65000,'guntur')

 

select * from EmployeeDetails

Once we run above script our table “EmployeeDetails” will create and result will be like as shown below

 

Newly created employeedetails table in sql server

Now run following examples to check BETWEEN operator in SQL Server

SQL Between Operator Example

The following SQL query will return all employees details where empid in between 1 and 3.

 

SELECT * FROM EmployeeDetails WHERE empid BETWEEN 1 and 3

Once we run above sql query our sql between operator result will be like as shown below

 

Output

 

Following is the output of sql between operator example

 

SQL Between Operator with Example Result or Output

SQL NOT BETWEEN Operator with Example

Generally in SQL statement if we use BETWEEN operator it will return record where value between within range. Suppose if we use NOT keyword with BETWEEN operator it will return data where column value not in between range of values.

 

Example 

 

In following SQL query we will see how to use NOT with BETWEEN operator in SQL. The following query will return all employees details where empname not in between 1 and 3 range

 

SELECT * FROM EmployeeDetails WHERE empid NOT BETWEEN 1 and 3

Once we run above sql query our sql not between operator result will be like as shown below

 

Output

 

Following is the result of using not keyword with between operator example in sql server

 

SQL Not Between Operator Example Result or Output

Now we will see how to use NOT keyword with IN operator and will check how it will return records. Generally if we use NOT keyword with IN operator it will return all the records that does not exists in list of values.

 

 

SQL BETWEEN Operator with Character (Text) Value Example

Generally in SQL statement if we use BETWEEN operator with character string values it will return all the records where the column name beginning with any letter between character string values

 

Example

 

The following SQL query will return all the records where the location name starts with any letter between 'A' and 'K'

 

SELECT * FROM EmployeeDetails WHERE Location BETWEEN 'A' AND 'K'

 Above sql query will return result for sql between operator with character will be like as shown below

 

Output

 

Following is the output of sql between operator with character value example in sql server

 

SQL Between Operator with Characters Example Output or Result

SQL NOT BETWEEN Operator with Character (Text) Value Example

Generally in SQL statement if we use BETWEEN operator with character values it will return all the records where the column name beginning with any letter between character string values. Suppose if we use NOT with BETWEEN operator it will return all records where column name not start between string values.

 

Example

 

The following SQL query will return all the records where the location name not begin with any letter between 'A' and 'K'

 

SELECT * FROM EmployeeDetails WHERE Location NOT BETWEEN 'A' AND 'K'

Once we run above sql query our sqn not between operator with character example result will be like as shown below

 

Output

 

Following is the output of sql not between operator with character value example

 

SQL NOT BETWEEN Operator with Character (Text) Value Example

SQL BETWEEN Operator with Date Value Example

Generally in SQL statement if we use BETWEEN operator with date values it will return all the records between range of date values. 

 

Example

 

The following SQL query will return all the records between ranges of date values ('1986-05-20', '1997-08-24')

 

SELECT * FROM EmployeeDetails WHERE joineddate BETWEEN '1986-05-20' AND '1997-08-24'

Once we run abouve query we will get result of sql between operator with date value will be like as shown below

 

Output

 

Following is the output of sql between operator with date value example

 

SQL BETWEEN Operator with Date Value Example Output or Result

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User