Home »
SQL
SQL Query to compare dates
SQL | Comparing dates: Here, we are going to learn how to compare dates in SQL? Here, we are writing query to compare the dates.
Submitted by Abhishek Goel, on May 02, 2020
For comparing dates in SQL, we use the DATE() function. The date should be in the DDMMYY format. The following is the syntax for the DATE() function.
Syntax:
SELECT * FROM TableName WHERE ColumName=Date;
To have a better understanding of the concept we will have the following code,
CREATE TABLE CompDate
(
Name VARCHAR(100),
Login DATE
);
INSERT INTO CompDate VALUES('John','22042019');
INSERT INTO CompDate VALUES('Bob','12052018');
INSERT INTO CompDate VALUES('Carol','10032016');
INSERT INTO CompDate VALUES('David','08081995');
SELECT * FROM CompDate;
Output
John|22042019
Bob|12052018
Carol|10032016
David|8081995
SELECT * FROM CompDate WHERE Login = '10032016';
Output
Carol|10032016
In the above statement, if we use the greater than sign:
SELECT * FROM CompDate WHERE Login > '10032016';
Output
John|22042019
Bob|12052018
Hence, we can conclude that dates can be compared in SQL using simple statements.