SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today’s date by comparing date with today’s date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern.
- This function is used to find the present date and time of the database system.
- This function comes under Date Functions.
- This function doesn’t accept any parameter.
- This function returns output in ‘YYYY-MM-DD hh:mm: ss. mmm‘ format.
To check a current date we use simply GETDATE( ) function.
Now, take an example to check if the date is greater than today’s date in MS SQL Server. For this we follow given below steps:
Step 1: Create a database
we can use the following command to create a database called geeks.
CREATE DATABASE geeks;
Step 2: Use database
Use the below SQL statement to switch the database context to geeks:
Step 3: Table definition
We have the following geeks for geeks in our geek’s database.
CREATE TABLE geeksforgeeks( NAME VARCHAR(20), Ordered DATE, Deliver DATE);
Step 4: Insert data into a table
INSERT INTO geeksforgeeks VALUES ('ROMY', '2021-01-16', '2021-03-12'), ('AVINAV', '2021-11-12', '2021-12-12'), ('PUSHKAR', '2021-06-23', '2021-10-13');
Step 5: For a view a table data
To see the content of the table, run the below command
SELECT * FROM geeksforgeeks;
Step 6: Check date greater than today date or not
- For this, we will check from the table, which row has delivered a value greater than today’s date.
SELECT * FROM geeksforgeeks WHERE Deliver > GETDATE();
Returned value whose date is 2021-12-12 and 2021-10-13 which is greater than 2021-09-22 (Today’s date)
- Check whose ordered date is greater than today’s date.
SELECT * FROM geeksforgeeks WHERE Ordered > GETDATE();