How to get Date Difference between Two Dates

Hi Experts,
We have table having following column

ID,systime,eventcode
ID = field autonumber
Systime =System Time
EventCode = 4,12 ( where 4= Start and 12= stop )

we need to calculate Vehicle working duration between two dates.

Kindly check the attached  picture file.
Snap1.png
Asrar AzmiWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
use datediff function

http://technet.microsoft.com/en-us/library/ms189794.aspx

select datediff (minutes, startDate, endDate  )

select datediff (minutes, (select sysdate from yourTable where eventID = 4 ) ,  (select sysdate from yourTable where eventID = 12 ) )
0
Brian CroweDatabase AdministratorCommented:
I'm making some assumptions that the data is consistent with alternating starts/stops...

WITH cteEvent (ID, sysTime, eventCode, eventNumber)
AS
(
   SELECT ID, systime, eventcode,
      ROW_NUMBER() AS EventNumber
   FROM gpssystem.dbo.gpshistory
   WHERE  devicenum = '359231039842834'
      AND eventcode IN (4, 12)
      and systime BETWEEN '20130910' AND '20130910 23:00'
   ORDER BY ID
)
SELECT Start.ID AS StartID, Stop.ID AS StopID,
   Start.sysTime AS StartTime, Stop.sysTime AS StopTime,
   DATEDIFF(minute, Start.sysTime, Stop.sysTime) AS ElapsedTime
FROM cteEvent AS Start
LEFT OUTER JOIN cteEvent AS Stop
   ON Start.eventCode = 4        --swap the eventCode values if i have them backwards
   AND Stop.eventCode = 12
   AND Start.eventNumber = Stop.eventNumber - 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
could be a big assumption

The information appears to be collected 24x7 or 24x5, so for ANY selection made involving a start point, a start event could be disassociated from its related stop event or vice-versa.

in fact row 1 of the image displays exactly that (an eventCode 12)

In that image you have asked for data >= '2013-09-10 00:00:00'
and the first row has a datetime of '2013-09-10 21:44:43:000'
>> does that mean the equipment was running between 00:00:00 and 21:44:43?


---------
whilst you have specified MS SQL 2008 as a topic, do you happen to be using SQL 2012?
{SQL 2012 has a feature that would be darn handy: lead()/lag() }
0
PortletPaulfreelancerCommented:
any chance of running this and providing the results as text please?

SELECT
        ID
      , devicenum
      , systime
      , eventcode
FROM gpssystem.dbo.gpshistory
WHERE devicenum = '359231039842834'
        AND eventcode IN (4, 12)
        AND systime >=   '2013-09-09 00:00:00'
        AND systime  <   '2013-09-11 00:00:00'

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.