BETWEEN conditions in SQL Server

In SQL Server (Transact-SQL), BETWEEN conditions are used to retrieve data in a range, in the SELECT, INSERT, UPDATE, or DELETE commands.

Syntax of BETWEEN conditions

 bieu_thuc BETWEEN giatri1 AND giatri2 ; 

Variable name or variable value

bieu_thuc

column or information field

giri1 and giatri2

values ​​that make up the range that will be compared, including the two end values.

Note

The BETWEEN condition will return all records that bieu_thuc is between girth1 and giri2, including 2 end values.

For example - numerical value

 SELE CT * 
FROM nhanvien
WHERE n hanvien_id BETWEEN 25 AND 100;

The above example will return rows in the table if nhanvien_id is between 25 and 100 (including 25 and 100), equivalent to the SELECT statement below.

 SELEC T * 
FROM nhanvien
WHERE nhanvien_id >= 25
AND nhanvien_i d <= 100;

Example - with date

 SEL ECT * 
FROM nhanvien
WHERE n gay_bat_dau BETWEEN '05/05/2014' AND '05/31/2014';

The above BETWEEN conditions return records from the table with the value immediately_bat_dau between May 1, 2014 and May 31, 2014 (including the first and last days). The above command is equivalent to the SELECT command below.

 SELEC T * 
FROM nhanvien
WHERE ngay_bat_dau >= '2014/05/01'
AND ngay _bat_dau <= '05/05/2014';

Example - NOT operator

 SELE CT * 
FROM nhanvien
WHERE nhanvien_id NOT BETWEEN 2000 AND 2999;

The SELECT statement returns rows from the table if nhanvien_id is not between 2000 and 2999, including the two end values, equivalent to the following command.

 SELE CT * 
FROM nhanvien
WHERE nhanvien_id < 2000
OR nhan vien_id> 2999;

Last lesson: JOIN in SQL Server

The following article: INSERT command in SQL Server

4 ★ | 1 Vote

May be interested

  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • How to set up your own Git server on LinuxHow to set up your own Git server on Linux
    while you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
  • Use IIS to set up FTP Server on WindowsUse IIS to set up FTP Server on Windows
    set up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • What is VPS? VPS used to do? What is VPS different from Server?What is VPS? VPS used to do? What is VPS different from Server?
    what is vps? vps used to do? what is vps different from server ?. when you intend to learn about network data or open the website, you will definitely be introduced to many different server and server services. but server hosting has a lot of tricks
  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • Instructions for setting up and managing FTP Server on Windows 10Instructions for setting up and managing FTP Server on Windows 10
    if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
  • Create VPN Server on Windows 8Create VPN Server on Windows 8
    no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
  • What is the future of server virtualization?What is the future of server virtualization?
    server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.