How to delete a row if date and time are older than a spacific date and time

  • Hi

    I want to have a row deleted using a SP when the the date and time is older than the current date and time ie: if a date and time is older that 8 am every morning then it should be deleted. I have a column (datein-[datetime] and timein [char].

    Any help would be appreciated

    Thanks

    Greg

  • Will this help ?

     

    DELETE A FROM TABLE_NAME A

    WHERE CONVERT(VARCHAR(8),DATEIN,112) < CONVERT(VARCHAR(8),GETDATE(),112)

    AND TIMEIN < 8


    Regards,

    Ganesh

  • Thank for this

    I get this error in QA

    Server: Msg 245, Level 16, State 1, Procedure CleanWho'sOutList, Line 10

    Syntax error converting the varchar value '6:15 a.m. ' to a column of data type int.

    Greg

  • Try this...

     

    DELETE A FROM TABLE_NAME A

    WHERE CONVERT(VARCHAR(8),DATEIN,112) < CONVERT(VARCHAR(8),GETDATE(),112)

    AND CAST(substring(TIMEIN,1,patindex('%:%',TIMEIN)-1) AS INT) < 8


    Regards,

    Ganesh

  • Cheers NewBie

    This worked but did leve and entry from 31/08/06 @ 9am

    Greg

  • Try this:

    Delete tableName where Cast(Convert(Char(10),DateIN,101) + ' ' + Cast(TIMEIN as varchar(10)) as datetime) <=  Cast(Convert(Char(10),getdate(),101) + ' 08:00:00' as Datetime)

    Sreejith

  • You can also try this...

     

    DELETE A FROM TABLE_NAME A

    WHERE CONVERT(VARCHAR(8),DATEIN,112) < CONVERT(VARCHAR(8),GETDATE(),112)

    DELETE A FROM TABLE_NAME A

    WHERE CONVERT(VARCHAR(8),DATEIN,112) = CONVERT(VARCHAR(8),GETDATE(),112)

    AND CAST(substring(TIMEIN,1,patindex('%:%',TIMEIN)-1) AS INT) < 8


    Regards,

    Ganesh

  • Thanks for all your help guys. Much appreciated.

    Greg

  • If you're never gonna exceed capasity of Excel spreadsheet you can use the approaches above. (Why you need SQL Server then?)

    But for real databases:

    1) you better don't separate date and time;

    2) while you have it saparated use at least 1 index of 2:

    DELETE FROM TABLE_NAME

    WHERE DATEIN < GETDATE() - CONVERT(datetime, TIMEIN, 108)

     

    _____________
    Code for TallyGenerator

  • Greg,

    It would appear that your TimeIn column could have the following formats...

    6:15 a.m.

    12:15 p.m.

    6:15 p.m.

    12:15 a.m.

    The periods in the "a.m." and "p.m." could make this a bugger to convert.  Even the COVERT 108 thing won't work.  You have to get rid of the periods before you can do anything with the time... the "a.m." and "p.m." are absolutely necessary, though, because the time is not 24 hour time.  So we need to keep them... and we force the date and time because you may decide to run this after 8 and not right at 8...

     DELETE yourtable

      WHERE @DateIn + REPLACE(@TimeIn,'.','')

          < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) + '08:00 AM'

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply