
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select Data Between Two Dates and Times in SQL Server
Sometimes, you need to get data from SQL Server that falls between two specific dates or times, like finding all orders placed within a week or all activities that happened during a certain time frame. Writing the right query can be challenging if you're not familiar with how to filter data by date and time.
In this article, we will show you how to easily select data between two dates and times in SQL Server using simple queries.
Steps to Write Queries for Selecting Data Between Dates and Times
As we're going to write queries to filter data between specific dates and times, here's what you need to know:
We'll be using the DATETIME format (YYYY-MM-DD HH:MM:SS) for dates and times. To filter data between two dates or times, we'll use the BETWEEN operator with WHERE. This will include both the start and end values. Alternatively, we can use >= and <= for custom ranges.
Step 1: Create a New Database
To start, create a new database named TutorialsPointDB using the following SQL statement:
CREATE DATABASE TutorialsPointDB; USE TutorialsPointDB;
Output

Step 2: Create the EmployeeOrders Table
Next, let's create a table named EmployeeOrders to store order data. The table will have the following columns: OrderID (INT), CustomerName (VARCHAR), and OrderDate (DATETIME). Here's the SQL code to create the table:
CREATE TABLE EmployeeOrders ( OrderID INT PRIMARY KEY, CustomerName VARCHAR(100), OrderDate DATETIME );
Step 3: Insert Sample Records
Now let's insert some sample records into the EmployeeOrders table. These records will include different dates and times:
INSERT INTO EmployeeOrders (OrderID, CustomerName, OrderDate) VALUES (1, 'John Doe', '2025-01-01 10:15:00'), (2, 'Jane Smith', '2025-01-02 14:30:00'), (3, 'Michael Johnson', '2025-01-05 09:00:00'), (4, 'Emily Davis', '2025-01-07 16:45:00'), (5, 'Sarah Brown', '2025-01-10 11:20:00');
Output

Step 4: Display Records
To display the records that were inserted into the EmployeeOrders table, you can use the following SELECT query:
SELECT * FROM EmployeeOrders;
Output

Step 5: Filter Records Between Two Dates
Now, let's filter records between two specific dates and times using the BETWEEN operator.
Example: Using the BETWEEN Operator
SELECT * FROM EmployeeOrders WHERE OrderDate BETWEEN '2025-01-02' AND '2025-01-07';
Output

Example: Using >= and <= Operators
SELECT * FROM EmployeeOrders WHERE OrderDate >= '2025-01-02 00:00:00' AND OrderDate <= '2025-01-07 23:59:59';
Output

Step 6: Filter Data by Time Only
If you want to filter based on time while ignoring the date, you can use the CAST function to extract only the time part of the OrderDate column.
Example: Select Orders Between Specific Times
SELECT * FROM EmployeeOrders WHERE CAST(OrderDate AS TIME) BETWEEN '09:00:00' AND '17:00:00';
Output

Step 7: Filter Data by Date Only
To filter records based only on the date (ignoring the time part), use the CAST function to convert OrderDate to a date.
Example: Select Orders Between Specific Dates
SELECT * FROM EmployeeOrders WHERE CAST(OrderDate AS DATE) BETWEEN '2025-01-01' AND '2025-01-07';
Output

Conclusion
You can easily select data between two dates or times in SQL Server using the BETWEEN operator or by using >= and <= for custom ranges. You can also filter by just the date or time using the CAST function to get the specific data you need.