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
VulpesPosted Oct 27, 2011, 11:00 AM
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.
Vinitha TPosted Nov 15, 2021, 12:30 PM
karthik vPosted Oct 27, 2011, 2:26 PM
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.
karthik vPosted Oct 27, 2011, 2:19 PM
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............
Suthish NairPosted Oct 27, 2011, 2:06 PM
SELECT Id FROM TABLE
WHERE DateCreated is not null
and DateCreated < DATEADD(month, -3, getdate())
If want to remove time, refer following methods.
Javeed M ShaikhPosted Oct 27, 2011, 11:52 AM
VulpesPosted Oct 27, 2011, 11:44 AM
Anyway, doubtless he'll clarify that in due course.
Javeed M ShaikhPosted Oct 27, 2011, 11:36 AM
VulpesPosted Oct 27, 2011, 11:14 AM
Javeed M ShaikhPosted Oct 27, 2011, 11:12 AM
SELECT Id FROM TABLE WHERE DateCreated is not null and DATEDIFF(month, ISNULL(DateCreated, DATEADD(month, -3, GetDate())), GetDate()) > 2