Difference between datetime

  • I have the following fields in my table.

    UserName DateTime Action

    abc 22/1/2011 23:45 xyz

    abc 23/1/2011 10:00 pqr

    abc 23/1/2011 15:00 pqr

    abc 23/1/2011 20:00 pqr

    abc 23/1/2011 21:00 pqr

    abc 23/1/2011 55:00 pqr

    If the user has not done any action in 30 min,I can say his session is ended.So i need to take be before that record and calculate timespan

  • Is this is a homework question? What have you tried so far? It really isn't clear what you require, in any case. Please also provide DDL in the form of a CREATE TABLE statement, sample data in the form of INSERT statements, and required results.

    John

  • Based on the data supplied I think the answer is as follows:

    declare @fdatetime datetime, @pdatetime datetime

    Select top 1 @ldatetime=datetime from <yourtablename> where username = <usernameyouare checking> order by datetime desc

    Select top 1 @pdatetime=datetime from <yourtablename> where username = <usernameyouare checking> and datetime < @ldatetime order by datetime desc

    If datediff(mi,@pdatetime,@ldatetime)> 30

    Begin

    <sessionendedlogic>

    End

    I am assuming you would put this in a stored procedure and either send in a username or run it without parameters and loop through a user table for all users checking every 30 minutes or something of that nature. Sorry I can't be more specific but the question was not real specific.

    HTH,

    DG

Viewing 3 posts - 1 through 2 (of 2 total)

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