SQL Query to Compare Two Dates Last Updated : 26 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article SQL Query to Convert Datetime to Epoch R romy421kumari Follow Improve Article Tags : SQL SQL-Server SQL-Query Similar Reads 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 How to Compare Time in MS SQL Server? 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 dat 3 min read How To Compare Only Date In Moment.js? In Moment.js, date manipulation and parsing can be done efficiently. Along with this, the library provides ways to compare only the date portion of two date objects, ignoring the time component. Methods like startOf, isSame, and formatting techniques enable you to focus solely on the date part for a 2 min read Like