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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.