
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Date is Greater Than Today in SQL
You can handle and manipulate date and time values within a database using SQL queries. You need to filter records based on date comparisons, like checking if a date is greater than today. We will query this using our own sample data as given below.
Date Comparisons
You can use various comparison operators, like >, <, =, >=, and <= to compare dates in SQL. You can retrieve records based on the relative values of date fields using these operators.
Syntax
The basic syntax to check if a date is greater than today in SQL is given as below
SELECT column1, column2, columnN FROM table_name WHERE date_column > CURRENT_DATE;
Here, date_column is the field containing the date values you want to compare. "CURRENT_DATE" is a keyword in SQL that represents the current date.
Example
Consider you have created a table named EVENTS using the CREATE TABLE statement as shown below
CREATE TABLE EVENTS ( EVENT_ID INT NOT NULL, EVENT_NAME VARCHAR(50) NOT NULL, EVENT_DATE DATE NOT NULL, LOCATION VARCHAR(50), PRIMARY KEY (EVENT_ID) );
Now, you can insert values into this table using the INSERT statement as given below
INSERT INTO EVENTS VALUES (1, 'Ramesh Meetup', '2024-12-15', 'Ahmedabad'), (2, 'Khilan Workshop', '2024-09-10', 'Delhi'), (3, 'Kaushik Seminar', '2022-11-25', 'Kota'), (4, 'Chaitali Webinar', '2021-10-05', 'Mumbai'), (5, 'Hardik Conference', '2024-09-20', 'Bhopal'), (6, 'Komal Symposium', '2019-09-30', 'Hyderabad'), (7, 'Muffy Hackathon', '2023-08-15', 'Indore'), (8, 'Ramesh Training', '2020-12-01', 'Ahmedabad'), (9, 'Khilan Lecture', '2024-10-15', 'Delhi'), (10, 'Komal Networking', '2021-11-10', 'Hyderabad');
Now, you can view it using below command
SELECT * FROM EVENTS;
The table will be created like this
EVENT_ID | EVENT_NAME | EVENT_DATE | LOCATION |
---|---|---|---|
1 | Ramesh Meetup | 2024-12-15 | Ahmedabad |
2 | Khilan Workshop | 2024-09-10 | Delhi |
3 | Kaushik Seminar | 2022-11-25 | Kota |
4 | Chaitali Webinar | 2021-10-05 | Mumbai |
5 | Hardik Conference | 2024-09-20 | Bhopal |
6 | Komal Symposium | 2019-09-30 | Hyderabad |
7 | Muffy Hackathon | 2023-08-15 | Indore |
8 | Ramesh Training | 2020-12-01 | Ahmedabad |
9 | Khilan Lecture | 2024-10-15 | Delhi |
10 | Komal Networking | 2021-11-10 | Hyderabad |
Retrieving Future Events
You can use the below SQL query to fetch all events that are scheduled after today
SELECT EVENT_NAME, EVENT_DATE FROM EVENTS WHERE EVENT_DATE > CURRENT_DATE;
The above query will produce a table listing only those events whose dates are after today
EVENT_NAME | EVENT_DATE |
---|---|
Ramesh Meetup | 2024-12-15 |
Khilan Workshop | 2024-09-10 |
Hardik Conference | 2024-09-20 |
Khilan Lecture | 2024-10-15 |
Comparing Dates Using Different Formats
If the dates in your table are stored in different formats, you may need to convert these dates to a standard date format before comparison. SQL has various date functions like DATE_FORMAT() and TO_DATE() (depending on the SQL dialect you are using) to handle different date formats.
Example
Suppose the "EVENT_DATE" in the EVENTS table is stored as a string in the format 'DD-MM-YYYY'. You can convert it to a date format and then compare it with the current date.
SELECT EVENT_NAME, EVENT_DATE FROM EVENTS WHERE STR_TO_DATE(EVENT_DATE, '%d-%m-%Y') > CURRENT_DATE;
Output
The query above converts the EVENT_DATE to the date format and then retrieves events with dates greater than today
EVENT_NAME | EVENT_DATE |
---|---|
Komal Networking | 10-11-2024 |
Kaushik Seminar | 25-11-2024 |
Ramesh Training | 01-12-2024 |
Ramesh Meetup | 15-12-2024 |
Aliasing Date Comparisons in SELECT Statement
You can have more meaningful output and make the result set more readable by aliasing the output of your date comparisons using the "AS" keyword.
Example
Below is an example where we alias the result of the date comparison as 'Future Events'
SELECT EVENT_NAME, EVENT_DATE AS 'Future Events' FROM EVENTS WHERE EVENT_DATE > CURRENT_DATE;
Output
This query produces the below table with an alias
EVENT_NAME | Future Events |
---|---|
Ramesh Meetup | 2024-12-15 |
Khilan Workshop | 2024-09-10 |
Hardik Conference | 2024-09-20 |
Khilan Lecture | 2024-10-15 |