---
title: "How to return \"YES\"/\"NO\" based on date comparison in SQL"  
description: "How to return \"YES\"/\"NO\" based on date comparison in SQL"  
author: "Revati S Misra"  
published: 2023-04-12  
updated: 2023-05-02  
canonical: https://www.mindstick.com/forum/157763/how-to-return-yes-no-based-on-date-comparison-in-sql  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How to return "YES"/"NO" based on date comparison in SQL

How to return "[YES](https://yourviews.mindstick.com/audio/1323/the-magic-of-saying-yes-to-spontaneous-adventures)"/"NO" based on [date comparison](https://www.mindstick.com/forum/33809/date-comparison-validation-in-android) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database)

## Replies

### Reply by Aryan Kumar

To return "YES" or "NO" based on date comparison in SQL, you can use a CASE statement with the DATEDIFF function to compare the two dates.

Here's an example query:

```php
SELECT CASE
        WHEN DATEDIFF(day, date1, date2) > 30 THEN 'YES'
        ELSE 'NO'
      END AS result
FROM student;
```

In this example, **date1** and **date2** are the two dates you want to compare, and **student** is the table where the dates are stored.

The DATEDIFF function calculates the difference between two dates in a specified unit (in this case, days). If the difference between the two dates is greater than 30 days, the query will return 'YES', otherwise it will return 'NO'.

You can adjust the comparison to suit your needs, such as comparing months, years, or even hours or minutes.

### Reply by Krishnapriya Rajeev

A CASE statement is used to return "YES" or "NO" based on a [date](https://yourviews.mindstick.com/story/3869/propose-day-2024-exciting-date-ideas-for-you-and-your-partner) [comparison](https://www.mindstick.com/articles/23182/comparison-maruti-suzuki-celerio-v-s-tata-tiago) in SQL. An example is demonstrated below:

```plaintext
SELECT
  CASE
    WHEN date1 > date2 THEN 'YES'
    ELSE 'NO'
  END AS result
FROM your_table;
```

date1 and date2 are the columns that require comparison in a table in the above example. The CASE statement checks whether date1 is greater than date2 and produces a result of YES if it is true, and NO otherwise.


---

Original Source: https://www.mindstick.com/forum/157763/how-to-return-yes-no-based-on-date-comparison-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
