SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
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
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

soni
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!!!!!!!!!!!

sql-programmers
Posting Yak Master

USA
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
DECLARE @diff AS INT

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
SELECT
(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
http://www.sql-programmers.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 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))

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



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000