Delete 4 hours old rows in MS SQL DB

Discussion in 'Databases' started by waseemsz, Dec 7, 2012.

  1. Hi Folks, I have a MS Sql server database with a table named PTable with the following fields:
    Timeofflight =smalldatetime ie
    departure= varchar(10) ie toronto
    arrivalcity=varchar(10) ie new york

    Timeofflight departure arrivalcity
    2012-07-12 12:42:00 London NewYork
    2012-07-12 11:42:00 London Toronto
    2012-07-12 08:42:00 Gatwick Montreal
    2012-07-12 07:42:00 Frankfurt Toronto
    2012-07-11 22:42:00 Milan London

    Present time getutcdate()
    I am trying to delete the rows which are 4 hours old. I tried many queries but not able to acompolish this task. So I am here to reqeuste for your help.
    Please give me a query which can select or delete the 4 hours old records.

    Your help will be much appreciated.
    regards, ws
     
  2. Elshadriel

    Elshadriel Winhost Staff

    SELECT * FROM PTable
    WHERE TimeOfFlight < DATEADD(hour, -4, GETUTCDATE())

    That will select all the rows that are more than 4 hours old.
     
  3. Thnx for your answer :) it helped me out a lot:D
     
  4. Elshadriel

    Elshadriel Winhost Staff

    You're welcome. :)
     

Share This Page