How to Select Data Between Two Dates and Times in SQL Server?
Last Updated :
19 Nov, 2021
In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database.
Note: Here, we will use the two dates and times given in the query and separate them using the BETWEEN keyword. This is preceded by the WHERE keyword for satisfying the condition generated by the BETWEEN clause. As stated above, the format of date and time in our table shall be yyyy:mm: dd hh:mm: ss which is implied by DATETIME2. The time is in a 24-hour format.
Syntax:
SELECT * FROM TABLE_NAME WHERE
DATE_TIME_COLUMN BETWEEN
'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table ATM inside the database GeeksForGeeks. This table has 3 columns namely HOLDER_NAME, WITHDRAWAL_AMOUNT, and TRANSACTION_TIME containing the name of the account holder, the amount he/she withdrew, and the date and time of the transaction.
Query:
CREATE TABLE ATM(
HOLDER_NAME VARCHAR(10),
WITHDRAWAL_AMOUNT INT,
TRANSACTION_TIME DATETIME2);
Output:

Step 4: Describe the structure of the table ATM.
Query:
EXEC SP_COLUMNS ATM;
Output:

Step 5: Insert 5 rows into the ATM table.
Query:
INSERT INTO ATM VALUES('BOB',300,'2001-01-10 10:40:50');
INSERT INTO ATM VALUES('MARY',400,'2001-03-27 11:00:37');
INSERT INTO ATM VALUES('VANCE',100,'2002-09-18 13:45:21');
INSERT INTO ATM VALUES('OSCAR',1000,'2005-02-28 21:26:54');
INSERT INTO ATM VALUES('PETER',200,'2008-12-25 00:01:00');
Output:

Step 6: Display all the rows of the ATM table.
Query:
SELECT * FROM ATM;
Output:

Step 7: Retrieve the details of the transactions done between 10:00 am, 1st February 2001 and 10:00 pm, 1st May 2007.
Query:
SELECT * FROM ATM WHERE
TRANSACTION_TIME BETWEEN
'2001-02-01 10:00:00' AND
'2007-03-01 22:00:00';
Output:

Step 8: Retrieve the details of the transactions done between 09:00 pm, 28th February 2005 and 12:00 am, 25th December 2008.
Query:
SELECT * FROM ATM WHERE TRANSACTION_TIME
BETWEEN '2005-02-28 21:00:00'
AND '2008-12-25 00:00:00';
Note - The 5th tuple is not displayed as its transaction time is 00:01:00 whereas our query has limited the time to 00:00:00.
Output:

Step 9: Retrieve the details of the transactions done between 10:30 am, 10th January 2001, and 12:00 pm, 27th March 2001.
Query:
SELECT * FROM ATM WHERE TRANSACTION_TIME
BETWEEN '2001-01-10 10:30:00'
AND '2001-03-27 12:00:00';
Output:
Similar Reads
How to Select Dates Between Two Date Range in PL/SQL In database management, PL/SQL (Procedural Language/Structured Query Language) stands out as a powerful tool for managing data within Oracle databases. When dealing with date-related queries, particularly selecting dates between two date ranges, PL/SQL offers robust functionality to streamline the p
4 min read
How to Select Dates Between Two Dates in PostgreSQL? When managing a PostgreSQL database, we may often encounter scenarios where we need to filter data based on date ranges. This task is crucial for generating reports, analyzing trends, or retrieving time-sensitive information. However, querying for data within a specific date range can be a challengi
4 min read
How to Get all Dates Between Two Dates in SQL Navigating and manipulating date ranges is a common task in SQL, especially when we need to generate lists of dates between two specified dates. This article covers two methods using the SQL GROUP BY clause: the traditional method with explicit column names and the shorter "GROUP BY 1" syntax. Weâll
4 min read
How to Remove Times from Dates in SQL Server In SQL Server, there are Date and DateTime data types to store Date and Time values. There can be situations when only the date value needs to be displayed or to do date calculations for date add, date difference functions, and between two dates from datetime values. So, to remove the Time part from
4 min read
How to Query Between Two Dates in MySQL? MySql is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
4 min read