Jump to content

delete some records...


Recommended Posts

I'd like to delete records which are older than six month.I found a description, telling me to write the query like this:Try Dim Conn as System.Data.SqlClient.SqlConnection = NewSystem.Data.SqlClient.SqlConnection("data source=cen database=date;user id=...; pwd=...") Dim Comm as System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("DELETE * FROM Tb_Table WHERE MyDate < DATEADD(mm, -6, current_timestamp)") Conn.Open() Comm.ExecuteNonQuery()Catch console.WriteLine("mistake")End TryMy Application runs until Comm.ExecuteNonQuery() and then runs into the Catch-Statement. Can anybody find a mistake?

Link to post
Share on other sites

This might be the case, I'm just beginning with programming. I use Visual Studio.Net, writing in VB. And my task is to connect to my database and delete every month those entries which are older than 6 month. I got the hint to do it like the way I posted before by a member of another forum. But this might be wrong, I have no idea...If you please could help me? Does SQL have those functions like DATEADD? Or how can I solve this task?

Link to post
Share on other sites
I'd like to delete records which are older than six month.I found a description, telling me to write the query like this:Try  Dim Conn as System.Data.SqlClient.SqlConnection = NewSystem.Data.SqlClient.SqlConnection("data source=cen database=date;user id=...; pwd=...")  Dim Comm as System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("DELETE * FROM Tb_Table WHERE MyDate < DATEADD(mm, -6, current_timestamp)")  Conn.Open()  Comm.ExecuteNonQuery()Catch  console.WriteLine("mistake")End TryMy Application runs until Comm.ExecuteNonQuery() and then runs into the Catch-Statement. Can anybody find a mistake?

If you want to delete the records that are 6 months old y not try the DATEDIFF command which is a lot easier. click below for the syntaxDatediff
Link to post
Share on other sites

Thank you for your help. Meanwhile I got the error in my code. My select statement was not right. It has to be...Dim Comm as System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("DELETE FROM Tb_Table WHERE MyDate < DATEADD(mm, -6, current_timestamp)")I only deletet the star in my query and suddenly it worked...Thanks and greetingsmkdeluxe

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...