Hi all, I want to delete some records which i inserted 5 days before.Hw to calculate the difference between two dates and delete the appropriate records. Pls help me. Thanks Jeen
Thanks for ur solution.. But this process should be done everday. So we cant enter a specific date. Im having some date values in the database.It should be compared with the current date and then delete.Actually im doing this based on a website project.So this query is needed for that
[quote user="Jeena"] Thanks for ur solution.. But this process should be done everday. So we cant enter a specific date. Im having some date values in the database.It should be compared with the current date and then delete.Actually im doing this based on a website project.So this query is needed for that [/quote] Read my previous reply
[quote user="FrankKalis"] DELETE <table> WHERE <your data column> = DATEADD(DAY, -5, '20071017') [/quote] Why dont you use Getdate() instead of coding the date? []
Actually my problem is that: I hav a table that contains a column Sdate. Values r entered from forms itself(.Net).The requirement is that i want to delete the records that r entered 5 days back. Today if i entered 5 records to database, that records should be deleted after 5 days. This is based on a website project in VB.Net. So im using SQl server for this purpose.
[quote user="Jeena"] Actually my problem is that: I hav a table that contains a column Sdate. Values r entered from forms itself(.Net).The requirement is that i want to delete the records that r entered 5 days back. Today if i entered 5 records to database, that records should be deleted after 5 days. This is based on a website project in VB.Net. So im using SQl server for this purpose. [/quote] Create a stored procedure with the mentioned statement and schedule it to run once a day. That should be fine.
In Vb.Net coding itself i wil write the query to delete the records that r 5days old. It wil be in the main page itself. So that the users can view the details correctly.This table is for storing the advertisments. thanks Jeena
If the SDate is entered from the user interface, you'll just have to trust that it's correct. And you might have people entering data from all over the world, and some user may still be on the previous calender day. If there is a hidden date column with a default of GETDATE(), then at least you can trust the clock of the computer hosting SQL Server.
<P mce_keep="true">[quote user="Madhivanan"] <P>[quote user="FrankKalis"] <P>DELETE <table><BR>WHERE <your data column> = DATEADD(DAY, -5, '20071017')</P><P>[/quote]</P><P>Why dont you use Getdate() instead of coding the date? <IMG alt=Wink src="http://sql-server-performance.com/Community/emoticons/emotion-5.gif"></P><P>[/quote]</P><P>Don't know. I guess I have just forgotten.</P>
[quote user="Jeena"] Hi all, I want to delete some records which i inserted 5 days before.Hw to calculate the difference between two dates and delete the appropriate records. Pls help me. Thanks Jeen [/quote] Before trying this query take a table backup 1 Delete from table where datediff(day,datecol,getdate())=5 2 Delete from table where datecol>=dateadd(day,datediff(day,0,getdate()),-5) and datecol<dateadd(day,datediff(day,0,getdate()),-4)
First of all, you need to know that you cannot delete rows (records) from a database, only from a table. Second, your table must have a column (field) where the date of insertion is recorded, because the database does not keep track of this type of detail. Assuming you have a table dbo.MyTable with an InsertDate column, then you could use: DELETE FROM dbo.MyTable T WHERE DATEDIFF(DAY, T.InsertDate, GETDATE()) = 5 Run the following query before the delete query, to make sure you're deleting the correct rows. SELECT T.* FROM dbo.MyTable T WHERE DATEDIFF(DAY, T.InsertDate, GETDATE()) = 5
I am trying to delete data that is 14 months old so I am trying to modify your command but it isn't working correctly. If I do the select anything that would be in the current year, it works, but if it goes to the previous year, then it doesn't pull back any results. Here is the command that I am trying to use. Thanks!!SELECT * FROM dbo.Table WHERE DATEDIFF (MONTH, Date_Column, GETDATE()) = 14