Link to home
Start Free TrialLog in
Avatar of tcmmaxt
tcmmaxtFlag for United States of America

asked on

how do you flag a sql 2005 server database record where there has been no action for 8 hours

I need to develop an sql query  to select a record (from  ATable below)  where  there  has been no action for 8 hours. Each  record  in ATable includes  a field called  requestDate(type datetime) and a field named statusname (varchar). When requestDate = requestDate + 8 hours and the statusname field reads “pending”.
I would like to update (set) the “sendemail” field to read “yes” Please update the sql code below to do this task.  The query will be run by other means and not part of this task.
thank you
Select  xyzID,
        requestDate,
        statusname,
        sendemail,
From ATable
Update .......
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Not sure I fully understand where you are going with this, but it would seem like you need to evaluate the value of the requestDate field to see if it is older than 8 hours ago.  Is that correct?

Is so, I would do something like this:
Select  xyzID,
        requestDate,
        statusname,
        sendemail
From ATable 
Where requestDate <= DateAdd(hh, -8, GetDate())

Open in new window


If you need this to do an update, then you would just do this:
Update ATable
Set sendemail = 'Yes'
Where requestDate <= DateAdd(hh, -8, GetDate())

Open in new window


Or without actually updating the table, you could do this in the query:
Select  xyzID,
        requestDate,
        statusname,
        case 
           when requestDate <= DateAdd(hh, -8, GetDate()) 
              then 'Yes' 
           else 'No'
        end as sendemail
From ATable 

Open in new window


Hope that helps!
SOLUTION
Avatar of Alpesh Patel
Alpesh Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@PatelAlpesh:

My reading of the question is that requestDate is the updatedate field and Asker is looking for the select/update query to use to track the record based on the current datetime.
Avatar of tcmmaxt

ASKER

thanks mwvisa1 (and all): the idea here is the requestDate field logs the time when the user makes a request for something.  After 8 hours if the data in the statusname field does not read "pending" . I would like to update (set) the “sendemail” field to read “yes” which somehow will fire off an email alerting others that 8 hours have elapsed and no one has responsed to the request. Of course I need to figure how to capture and reset  “sendemail” field to read "no".
I need the first 2 scripts since i have to update eact time and somehow reset the fields after email fire off?

Select  xyzID,
        requestDate,
        statusname,
        sendemail
From ATable
Where requestDate <= DateAdd(hh, -8, GetDate())

Update ATable
Set sendemail = 'Yes'
Where requestDate <= DateAdd(hh, -8, GetDate())
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial