How to Compare Time in MS SQL Server?
Last Updated :
02 Jul, 2024
To compare time in MS SQL Server, use the comparison operators (=,<,>, etc.). In this article, we will be making use of the Microsoft SQL Server as our database. and comparing times using both pre-defined dates and the current date and time with the GETDATE()
function.
First, let's create a database named "geeks" in MS SQL Server and then compare times.
Query:
Creating Database:
CREATE DATABASE geeks;
USE geeks;
Here, we will create two variables of datetime, that we need to compare.
Compare Time in MS SQL Server
A simple way to compare time MS SQL Server is by using comparison operators. The time should be in datetime format and then it can be compared to each other correctly.
To convert a value into datetime value, use the CONVERT() function.
Syntax:
DECLARE @input1 DATETIME;
DECLARE @input2 DATETIME;
SELECT @input1 = CONVERT(VARCHAR(20), CONVERT(DATETIME, 'date and time'), 114);
SELECT @input2 = CONVERT(VARCHAR(20), CONVERT(DATETIME, 'date and time'), 114);
IF @input1 <= @input2
PRINT '@input1 less then @input2'
ELSE
PRINT '@input1 more then @input2';
Examples of Comparing Time in MS SQL Server
Let's look at some examples on how to compare time in MS SQL Server. There are two ways in which we can compare time in MS SQL Server. These are:
1. Using pre-defined date and time.
In MS SQL Server, the query can be designed to compare two or more datetime variables using just the "if" statements.
Example:
DECLARE @day1 DATETIME;
DECLARE @day2 DATETIME;
SELECT @day1 = CONVERT(VARCHAR(20), CONVERT(DATETIME, '2019-02-11 08:00:00'), 114);
SELECT @day2 = CONVERT(VARCHAR(20), CONVERT(DATETIME, '2020-02-11 08:00:00'), 114);
IF @day1 <= @day2
PRINT '@day1 less then @day2'
ELSE
PRINT '@day1 more then @day2';
Output :

Explanation: In this result, @day1
is less than @day2
, so the first condition is met and the corresponding message is printed.
2. Using GETDATE() to compare with current date and time.
The GETDATE() function in SQL can also be used to fetch current date and time, and then it can be compared to a given time value.
Example:
DECLARE @day1 DATETIME;
DECLARE @day2 DATETIME;
SELECT @day1 = CONVERT(VARCHAR(20), GETDATE(), 114);
SELECT @day2 = CONVERT(VARCHAR(20), CONVERT(DATETIME, '2019-02-11 08:00:00'), 114);
IF @day1 <= @day2
PRINT '@day1 less than @day2'
ELSE
PRINT '@day1 more than @day2';
Output:

Explanation: In this result, @day1
(current date and time) is more than @day2
, so the second condition is met and the corresponding message is printed.
Points to Remember
- Use standard comparison operators (
=
, <
, >
, <=
, >=
, <>
) to compare datetime values. - The
CONVERT
function is used to change the format of the datetime values for comparison. - Use
GETDATE()
to retrieve the current date and time for comparisons. - The
IF
statement is used to evaluate conditions and execute corresponding actions.
Similar Reads
How to compare time in R?
R programming Language supports both date and DateTime objects using various different formats and specifiers. The built-in framework as.Date function is responsible for the handling of dates alone, the library chron in R Programming handles both dates and times, without any support for time zones;
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 Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
CURRENT_TIMESTAMP() Function in SQL Server
In SQL Server, the CURRENT_TIMESTAMP function is a widely used feature for retrieving the current date and time directly from the database server. It returns the exact moment when the SQL query is executed in the YYYY-MM-DD hh:mm:ss.mmm format.In this article, We will learn about CURRENT_TIMESTAMP()
3 min read
How to Enable SQL Server Query Logging?
In the domain of database management, the SQL query is found to be very helpful in improving performance, safety, and diagnosing problems. The query logging SQL Server provides help in that administrators of database servers can view and analyze the queries executed against their servers. Query logg
4 min read
SQL Query to Compare Two Dates
In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. Here we will see, SQL Query to compa
2 min read
How to Convert DateTime to VarChar in SQL Server
In SQL Server, date and time values are often stored in the DATETIME or DATE data types. However, there are situations where we may want to convert these values into a different format such as VARCHAR to display the date in a specific format or for further manipulation.In this article, we will explo
4 min read
How to Strip Time Component From Datetime in MySQL?
MySQL is a very popular open-source and free database server that is ideal for small and large applications and is a relational database management system where SQL (Structured Query Language) queries or statements are used to manage and manipulate the data. MySQL provides many built-in functions li
4 min read
How to Compare Rows and Columns in the Same Table in SQL
In SQL, comparing rows and columns in the same table is a common task for data analysis, identifying relationships, and calculating differences. By using techniques like self-joins and conditional queries, we can extract meaningful insights from a single table. This enables efficient querying, allow
4 min read
How to Select Data Between Two Dates and Times in SQL Server?
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 a
2 min read