Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if DateTime in DB is more than 90 days old via Stored Procedure

UPDATE

Evidently I didn't include enough data, sorry!

What I need to do is set 'campaign_Status' = 6 when 'campaign_Date' is more than 90 days old.


Hi,

I have a column (campaign_Date) which stores a DATETIME. Using a Stored Procedure I need to check if the stored date is 90 days old (or more).

Any help would be great.

Thanks.

like image 899
Munklefish Avatar asked Sep 28 '09 13:09

Munklefish


People also ask

How do I get 30 days old data in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How do I get last 3 months data in SQL query?

In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records.

How do you check a date is less than current date in SQL Server?

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.

How do I check if a date is greater than today in SQL?

GETDATE() function: This function is used to return the present date and time of the database system. After comparison column contains the following string: Lesser than- If the date is less than today's date. Greater- If the date is greater than today's date.


2 Answers

This will return all old campaigns:

SELECT  *
FROM    mytable
WHERE   campaign_Date <= DATEADD(day, -90, GETDATE())

This will select 1 if campaign is old, 0 otherwise:

SELECT  CASE WHEN campaign_Date <= DATEADD(day, -90, GETDATE()) THEN 1 ELSE 0 END
FROM    mytable

Note that the first query condition is sargable: it will allow using an index to filter the dates.

This will update all old campaigns with status 6:

UPDATE  mytable
SET     campaign_status = 6
WHERE   campaign_Date <= DATEADD(day, -90, GETDATE())
like image 199
Quassnoi Avatar answered Nov 10 '22 01:11

Quassnoi


See the DateAdd function

http://msdn.microsoft.com/en-us/library/ms186819.aspx

SELECT *
FROM MyTable
WHERE Campaign_Date <= DateAdd (d, -90, GetDate())
like image 39
Raj More Avatar answered Nov 10 '22 00:11

Raj More