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.

Features:

  • 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.

Query:

SELECT GETDATE();        

Output:

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 Beginner.

Query:

CREATE DATABASE Beginner;

Step 2: Use database

Use the below SQL statement to switch the database context to Beginner:

Query:

USE Beginner;

Step 3: Table definition

We have the following Beginner for Beginner in our geek’s database.

Query:

CREATE TABLE w3wiki(
NAME VARCHAR(20),
Ordered DATE,
Deliver DATE);

Step 4: Insert data into a table

Query:

INSERT  INTO w3wiki 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

Query:

SELECT * FROM w3wiki;

Output:

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.

Query:

SELECT * FROM w3wiki WHERE Deliver > GETDATE();

Output:

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.

Query:

SELECT * FROM w3wiki WHERE Ordered > GETDATE();

Output:


Contact Us