Solved

get dates difference in hours and minutes

Posted on 2009-07-09
5
181 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
5 Comments
 
LVL 39

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 39

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 59

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now