I have a simple application which stores the data into the sql server database table named student table .The design of the table is as follows 3 colums. name ,sex and registeredtime(datetime).In my WPF window i have three fields to insert the data into the table.Now i want to have the delete button based on the input given by the user(which is a datetimepicker).
How to delete the data from the table which is 7 days old compared to the date given by the user. ?
I want to have a stored procedure which i can call from the c# code.i am able to try some thing like this but Select * from studenttable where registereddate < GetDate()-7 but i am unable to achieve what i am supposed to ...
You will probably need to use the DATEADD function:
DELETE StudentTable
WHERE DATEADD(day,-7,GetDate()) > registeredDate
Make sure you do the equivalent select first to make sure you are deleting what you want:
SELECT * FROM StudentTable
WHERE DATEADD(day,-7,GetDate()) > registeredDate
Your stored proc would look something like this:
CREATE PROCEDURE DeleteRecent AS
BEGIN
    DELETE StudentTable
    WHERE DATEADD(day,-7,GetDate()) > registeredDate
END
GO
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With