Thursday, 12 July 2018

SQL Server BETWEEN

'Between' operator is a range selector, your output will be filter between from range and  to range column values. 

The syntax is
 

expression between  value1 to  value2

Between operator work on any type of SQL query like , select,update ,delete ,join,group by ,distinct etc. Between operator mainly work on numeric ,integer ,decimal values, but it also
work on date values.

Here , we have created a table and inserted data to the table for example purpose.


CREATE TABLE student
(
        id INT NOT NULL,
        name VARCHAR (500) NOT NULL,
        age INT NOT NULL,
        adress VARCHAR (500) NOT NULL,
       date_of_birth DATETIME NOT NULL,
       height DECIMAL(10,2) NOT NULL
)

       INSERT INTO student(id,name,age,adress,date_of_birth,height)
       SELECT 1,'John1',12,'34,student street','01/08/2018',10.5
       UNION
       SELECT 1,'John2',13,'35,student street','02/08/2018',11
       UNION
       SELECT 1,'John3',14,'36,student street','03/08/2018',11.5
       UNION
       SELECT 1,'John4',15,'37,student street','04/08/2018',12
       UNION
       SELECT 1,'John5',16,'38,student street','05/08/2018',12.5
       UNION
       SELECT 1,'John6',17,'39,student street','06/08/2018',13.5
       UNION
       SELECT 1,'John7',18,'40,student street','07/08/2018',14
       UNION
       SELECT 1,'John8',19,'41,student street','08/08/2018',14.5
       UNION
       SELECT 1,'John9',20,'42,student street','09/08/2018',15
       UNION
       SELECT 1,'John10',21,'43,student street','10/08/2018',15.5
       UNION
       SELECT 1,'John11',22,'44,student street','11/08/2018',16



1)Here is the example of between operator with integer valueExample

SELECT name,adress FROM student
         WHERE age BETWEEN 13 AND 20

Output
name     adress
John2    35,student street
John3    36,student street
John4    37,student street
John5    38,student street
John6    39,student street
John7    40,student street
John8    41,student street
John9    42,student street 


2)Here  is there example of between operator with decimal values
Example


SELECT name,adress FROM student
WHERE height BETWEEN 13.5 AND 15

Output
name     adress 
John6    39,student street
John7    40,student street
John8    41,student street
John9    42,student street


3)Here  is the example of between operator with date values

Example

SELECT name,adress FROM student
WHERE date_of_birth BETWEEN '05/08/2018' AND '10/08/2018'

Output
name     adress 
John10   43,student street
John5    38,student street
John6    39,student street
John7    40,student street
John8    41,student street
John9    42,student street 



4)Here is example of between operator with update statement

Example
UPDATE student
SET name=name+'-'+CONVERT(VARCHAR,age)
WHERE date_of_birth BETWEEN '08/08/2018' AND '09/08/2018'



SELECT name,adress FROM student
WHERE date_of_birth BETWEEN '08/08/2018' AND '09/08/2018'

name     adress
John8-19    41,student street
John9-20    42,student street




No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...