Hi Experts
I have a problem with writing a sql query
I have a table in my database which holds the datetime stamp by name DateCreated(Column Name)
I have other column in my table which is Id.
I wanted to retrieve the Id Records which the DateCreated is not null and DateCreated should be less than three months
Here is the format of my Date 6/24/2011 10:56:03 AM
Any suggestions
Please Help
Loading

VulpesRank #55 · 29.2K reputationPosted Oct 27, 2011, 7:00 AM EDT
SELECT Id FROM TABLE WHERE DATEDIFF(month, ISNULL(DateCreated, DATEADD(month, -3, GetDate())), GetDate()) < 3
As you'll see, if DateCreated is NULL, it's replaced by a date 3 months ago and so shouldn't then feature in the query set.
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 27, 2011, 7:12 AM EDT
SELECT Id FROM TABLE WHERE DateCreated is not null and DATEDIFF(month, ISNULL(DateCreated, DATEADD(month, -3, GetDate())), GetDate()) > 2
karthik v115 reputationPosted Oct 27, 2011, 10:19 AM EDT
What i need to do is delete the records in the database which are more than 3 months old .
So i tried few queries which i created but none of them worked for me.At that point of time i was trying to write a query for retrieving the records from the database which are less than three months old .
And now i have the different ways of writing the same query which really improves my knowledge base.
Thanks to you guys
You Rock............
karthik v115 reputationPosted Oct 27, 2011, 10:26 AM EDT
i tried to query your solution but the query was returning the Id which are older than 3 months.
I modified the query with changing the less than symbol to greater than symbol and it worked as the Datecreated should be greater than the comparison Date
Correct me if am wrong.
I am still a beginner.
Vinitha TRank #8,005 · 230 reputationPosted Nov 15, 2021, 7:30 AM EST
VulpesRank #55 · 29.2K reputationPosted Oct 27, 2011, 7:14 AM EDT
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 27, 2011, 7:36 AM EDT
VulpesRank #55 · 29.2K reputationPosted Oct 27, 2011, 7:44 AM EDT
Anyway, doubtless he'll clarify that in due course.
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 27, 2011, 7:52 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 27, 2011, 10:06 AM EDT
SELECT Id FROM TABLE
WHERE DateCreated is not null
and DateCreated < DATEADD(month, -3, getdate())
If want to remove time, refer following methods.