---
title: "Writing a Query to Update Records Based on a Condition in SQL Server"  
description: "Writing a Query to Update Records Based on a Condition in SQL Server"  
author: "Anubhav Sharma"  
published: 2024-07-15  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160913/writing-a-query-to-update-records-based-on-a-condition-in-sql-server  
category: "SQL Server"  
tags: ["sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# Writing a Query to Update Records Based on a Condition in SQL Server

Hi all,

I need to [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) the `Status` [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) in my `Orders` table to 'Completed' for all [orders](https://answers.mindstick.com/qa/43206/what-were-the-fundamental-orders-of-connecticut-and-who-wrote-them) that `OrderDate` are older than 30 days. Can someone provide the SQL query for this?

## Replies

### Reply by Ravi Vishwakarma

To update the `Status` column in your `Orders` table to '**Completed**' for all orders where the `OrderDate` is older than 30 days, you can use the `UPDATE` statement with a `WHERE` clause.

Here is the query:

```plaintext
--using statement
UPDATE Orders
SET [Status] = 'Completed'
WHERE OrderDate < DATEADD(DAY, -30, GETDATE());

-- Using one time variable
Declare @LastDate DateTime = DATEADD(DAY, -30, GETDATE());
UPDATE Orders
SET [Status] = 'Completed'
WHERE OrderDate < @LastDate;

```

### Explanation:

- `UPDATE Orders`: Specifies the table to update.
- `SET Status = 'Completed'`: Sets the `Status` column to 'Completed'.
- `WHERE OrderDate < DATEADD(DAY, -30, GETDATE())`: Filters the rows to update where the `OrderDate` is more than 30 days older than the current date.

   - `GETDATE()`: Returns the current date and time.
   - `DATEADD(DAY, -30, GETDATE())`: Subtracts 30 days from the current date, giving you the date 30 days ago.

This query will update `Status` all orders placed more than 30 days ago to 'Completed'.

## Read more

[**How to Write a Query to Find the Second Highest Salary in SQL Server?**](https://www.mindstick.com/forum/160917/how-to-write-a-query-to-find-the-second-highest-salary-in-sql-server)

[**SQL Query to Pivot Data from Rows to Columns in SQL Server**](https://www.mindstick.com/forum/160916/sql-query-to-pivot-data-from-rows-to-columns-in-sql-server)

[**Help with Writing a Query to Find Orphaned Records in SQL Server**](https://www.mindstick.com/forum/160915/help-with-writing-a-query-to-find-orphaned-records-in-sql-server)

[**How to Write a Query to Get Top N Records per Group in SQL Server?**](https://www.mindstick.com/forum/160912/how-to-write-a-query-to-get-top-n-records-per-group-in-sql-server)

[**SQL Query to Find Duplicate Records in a Table in SQL Server**](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160913/writing-a-query-to-update-records-based-on-a-condition-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
