Need to get all rows in a table based on hour difference > 2 condition

  • Friends,

    How to get all the rows in the below mentioned table that matching the condition of checkInTime less than 2 hours of currenttime?

    TableName -> EducationDepartment

    Columns -> empID, checkInTime (datetime).

    I need to get all the employee ID's who's working hour is greater than 2 hours i.e GetDate() - checkInTime > 2.

    Please help me on this.

  • sunil.si (12/11/2008)


    Friends,

    How to get all the rows in the below mentioned table that matching the condition of checkInTime less than 2 hours of currenttime?

    TableName -> EducationDepartment

    Columns -> empID, checkInTime (datetime).

    I need to get all the employee ID's who's working hour is greater than 2 hours i.e GetDate() - checkInTime > 2.

    Please help me on this.

    select empID from EducationDepartment where empId in (select distinct empID from EducationDepartment where Datepart(hour, getdate())<2 and empID is not null)

    hope that can help you.

  • Select empid,checkintime from EducationDepartment

    WHERE DATEDIFF(ss,checkintime,getdate()) < 7200.

    I have converted 2 hrs into seconds to get as much correct results as possible.

    "Keep Trying"

  • Thanks friends, & it worked!

  • Can anyone help to find the syntax error in this query?

    @query = 'select empID from EducationDepartment where DateDiff(hh, checkInTime, getdate()) > 2 and userPriority = 1 and status != "closed"'

    Thx,

    Linus

  • sunil.si (12/12/2008)


    Can anyone help to find the syntax error in this query?

    @query = 'select empID from EducationDepartment where DateDiff(hh, checkInTime, getdate()) > 2 and userPriority = 1 and status != "closed"'

    Thx,

    Linus

    Probably the doublequotes here status != "closed"'

    But more importantly - why are you running this as dynamic sql? It should run just fine as it is.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi Chris,

    u r right & the double quotes created the problem... thx for the help!...

    Thx,

    Linus

Viewing 7 posts - 1 through 6 (of 6 total)

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