[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

get dates difference in hours and minutes

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
arcross
Asked:
arcross
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
ignore the first, use the cast.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT CAST(Col1 as datetime ) - Cast(Col2 as datetime )
0
 
Kevin CrossChief Technology OfficerCommented:
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
 
arcrossAuthor Commented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now