Solved

get dates difference in hours and minutes

Posted on 2009-07-09
5
201 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 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Record open by another user 6 58
MS SQL order by with "over" statement and row_number() 11 48
How to place a condition in a filter criteria in t-sql (#2)? 10 47
Help  needed 3 23
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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