Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Delete records after certain period

Delete records after certain period

Scheduled Pinned Locked Moved Database
databasemysqltutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alex barylski
    wrote on last edited by
    #1

    I have a table which requires records to be deleted programmatically at time intrvals. I have an created field which stores the time it was created and I can't figure out how to delete records older than 3 hours. ie: DELETE FROM temp WHERE created < now()+(3600*3) I'm using mysql BTW, but I don't think SQL or SQL would have much discrepancies here would they? Thanks for you time :) "Two wrongs don't make a right, but three lefts do!" - Alex Barylski :)

    M H 2 Replies Last reply
    0
    • A alex barylski

      I have a table which requires records to be deleted programmatically at time intrvals. I have an created field which stores the time it was created and I can't figure out how to delete records older than 3 hours. ie: DELETE FROM temp WHERE created < now()+(3600*3) I'm using mysql BTW, but I don't think SQL or SQL would have much discrepancies here would they? Thanks for you time :) "Two wrongs don't make a right, but three lefts do!" - Alex Barylski :)

      M Offline
      M Offline
      mwilliamson
      wrote on last edited by
      #2

      DELETE FROM temp WHERE created <= DATEADD( h, CURDATE(), -3); This will work for SQL Server, but I'm not sure about MySQL. Give it a shot :)

      1 Reply Last reply
      0
      • A alex barylski

        I have a table which requires records to be deleted programmatically at time intrvals. I have an created field which stores the time it was created and I can't figure out how to delete records older than 3 hours. ie: DELETE FROM temp WHERE created < now()+(3600*3) I'm using mysql BTW, but I don't think SQL or SQL would have much discrepancies here would they? Thanks for you time :) "Two wrongs don't make a right, but three lefts do!" - Alex Barylski :)

        H Offline
        H Offline
        Hesham Amin
        wrote on last edited by
        #3

        this solution can work for SQL Server: DELETE FROM temp WHERE DATEDIFF(Hour,created,getdate()) > 3

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups