SQL Query to Compare Two Dates
Last Updated :
26 Sep, 2021
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 compare two dates. This can be easily done using equals to(=), less than(<), and greater than(>) operators. In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement.
Syntax:
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
We can declare variables easily by using the keyword DECLARE before the variable name. By default, the local variable starts with @.
Syntax:
DECLARE @variable_name datatype;
Set values to the variable: We can assign values to the variables using the SET keyword.
Syntax:
SET @variable_name;
Now we take different cases to demonstrate of comparison between dates.
Query 1:
DECLARE @date1 DATE, @date2 DATE;
SET @date1='2021-01-01';
SET @date2='2021-02-02';
IF @date1=@date2
SELECT 'equal date'
ELSE
IF @date1<@date2 SELECT 'date2 is greater'
ELSE SELECT 'date1 is greater';
Output:

Query 2:
DECLARE @date1 DATE, @date2 VARCHAR(20);
SET @date1='2021-01-01';
SET @date2='2021-01-01';
IF @date1=@date2
SELECT 'equal date'
ELSE
IF @date1<@date2 SELECT 'date2 is greater'
ELSE SELECT 'date1 is greater';
Output:

Query 3:
DECLARE @date1 DATE, @date2 VARCHAR(20);
SET @date1='2022-01-01';
SET @date2='2021-01-01';
IF @date1=@date2
SELECT 'equal date'
ELSE
IF @date1<@date2 SELECT 'date2 is greater'
ELSE SELECT 'date1 is greater';
Output:
Similar Reads
SQL Query to Convert Date to Datetime In this article, we will look at how to convert Date to Datetime. We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type.Conve
1 min read
SQL Query to Compare Results With Today's Date In SQL, comparing results with today's date is a powerful tool for filtering data, managing schedules, including managing tasks, appointments and performing time-sensitive analysis. By using SQL's GETDATE() function we can easily perform this comparison. The ability to filter records based on date c
4 min read
SQL Query to Convert Datetime to Epoch Converting a datetime value to Epoch time is a common operation in SQL, particularly when working with timestamps in various applications. In this article, We will learn a step-by-step process of creating a SQL database, inserting datetime values and converting those values into Epoch time using SQL
3 min read
SQL Query to find All Sundays Between Two Dates To find all the Sundays in between two days using SQL Language, we will be using the "Date Functions" defined in SQL. Apart from these we will be using CTE ( View) idea too. Basic idea: So basically we are given two days, and we are required to list all Sundays between these two days. Thinking a lit
2 min read
How to Compare Dates with Moment.js? Moment.js is a popular JavaScript library that developers use for parsing, manipulating, and formatting dates and times. It provides a range of methods to compare dates, making it easier to handle various date-related tasks. With Moment.js, you can use methods like isBefore, isAfter, diff, and isSam
3 min read