Solved

get dates difference in hours and minutes

Posted on 2009-07-09
5
224 Views
Last Modified: 2012-05-07
Hello, ive got to date fields in my db with date and time and id like another column showing the difference in hours an minutes. in hte format hh:mm
0
Comment
Question by:arcross
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 24813783
SELECT  TO_CHAR(TO_DATE(TRUNC(MOD(date2-date1,1)*86400),'SSSSS'), 'HH24:MI:SS') time
from TABLE

cast ((datediff(hh,begin_time,end_time)) + ':' + (datediff(mm,begin_time,end_time)) as varchar(10))
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 24813787
ignore the first, use the cast.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 24813796
SELECT CAST(Col1 as datetime ) - Cast(Col2 as datetime )
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 24814463
Hello arcross,

See attached code snippet for another possible solution using MS SQL Server.

Regards,

mwvisa1
DECLARE @begin_date DATETIME, @end_date DATETIME
SET @begin_date = '2009-07-09 06:35'
SET @end_date = '2009-07-09 09:45' 
-- get results in minutes; convert to date as minutes from midnight; then use convert code 108 HH:mm format to display
SELECT CONVERT(VARCHAR(5), DATEADD(mi, DATEDIFF(mi, @begin_date, @end_date), 0), 108)

Open in new window

0
 
LVL 8

Author Comment

by:arcross
ID: 24818255
these two work...thanks to aneeshattingal (a little format there) and mwvisa1
1) SELECT CONVERT(varchar(5),endtime - starttime,108) from timesheet

2) SELECT starttime,endtime,CONVERT(VARCHAR(5), DATEADD(mi, DATEDIFF(mi, starttime, endtime), 0), 108) FROM timesheet

ged325 yours gave me this error..
"Conversion failed when converting the varchar value ':' to data type int."


thanks everyone!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question