Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 subtract dates of 12 hr format and extract time
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 10/15/2012 :  01:38:51  Show Profile  Reply with Quote
i have 2 date columns date1 and date2(date1- date2) which has date in 12 hr format
for ex :

Date1 : 10/3/2012 12:02:16 PM (mm/dd/yyyy hh:mm:ss)
Date2 : 9/21/2012 12:01:32 AM (mm/dd/yyyy hh:mm:ss)

i need to get the following
output: 300:00:44 (hh:mm:ss)

Plz help

Thnkz in advance!!!!!!!!!!!

Posting Yak Master

190 Posts

Posted - 10/15/2012 :  01:43:34  Show Profile  Visit sql-programmers's Homepage  Reply with Quote
declare @date1 AS DATETIME
declare @date2 AS DATETIME

SET @date1 = CONVERT(datetime, '10/3/2012 12:02:16 PM')
SET @date2 = CONVERT(datetime, '9/21/2012 12:01:32 AM')

SET @diff = DATEDIFF(SS, @date1, @date2)
SET @diff = CASE WHEN @diff < 0 THEN @diff * -1 ELSE @diff END
(CASE WHEN LEN((@diff/3600))>1 THEN CONVERT(VARCHAR, (@diff/3600)) ELSE '0' + CONVERT(VARCHAR, (@diff/3600)) END) + ':' +
(CASE WHEN LEN(((@diff%3600)/60))>1 THEN CONVERT(VARCHAR, ((@diff%3600)/60)) ELSE '0' + CONVERT(VARCHAR, ((@diff%3600)/60)) END ) + ':' +
(CASE WHEN LEN(((@diff%3600)%60))>1 THEN CONVERT(VARCHAR, ((@diff%3600)%60)) ELSE '0' + CONVERT(VARCHAR, ((@diff%3600)%60)) END )

SQL Server Programmers and Consultants
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 10/15/2012 :  11:12:15  Show Profile  Reply with Quote

declare @Date1 datetime ='10/3/2012 12:02:16 PM'
declare @Date2 datetime = '9/21/2012 11:01:32 PM'

SELECT STUFF( CONVERT(varchar(8),timediff,108),1,CHARINDEX(':',CONVERT(varchar(8),timediff,108))-1,DATEDIFF(dd,0,timediff) * 24 + DATEPART(hh,timediff))

SELECT DATEADD(ss,ABS(DATEDIFF(ss,@Date1,@Date2)),0) AS timediff

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000