Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Create function [dbo].[fn_TimeDiff](@ClassicDate datetime,@NowDate datetime)
RETURNS @Table Table (HoursDiff float,MinuteDiff float,SecondDiff float,MilliSecondDiff float)
AS
Begin
DECLARE @NowMilliSeconds float
Declare @ClassicMilliSeconds float
DECLARE @HourDiff float
DECLARE @MinuteDiff float
DECLARE @SecondDiff float
DECLARE @MilliSecondDiff float
DECLARE @GrandDiffInMilliSeconds float
set @NowMilliSeconds =0
set @ClassicMilliSeconds =0
set @HourDiff =0
set @MinuteDiff =0
set @SecondDiff =0
set @MilliSecondDiff =0
set @GrandDiffInMilliSeconds =0


SET @NowMilliSeconds=(DATEPART(Hour,@NowDate)*3600000)+
                    (DATEPART(MINUTE,@NowDate)*60000)+
                    (DATEPART(SECOND,@NowDate)*1000)+
                     DATEPART(MILLISECOND,@NowDate)
                     
 SET @ClassicMilliSeconds=(DATEPART(Hour,@ClassicDate)*3600000)+
                    (DATEPART(MINUTE,@ClassicDate)*60000)+
                    (DATEPART(SECOND,@ClassicDate)*1000)+
                     DATEPART(MILLISECOND,@ClassicDate)
                     
 IF((@NowMilliSeconds-@ClassicMilliSeconds)>0)
 BEGIN                    
	 SET @GrandDiffInMilliSeconds=@NowMilliSeconds-@ClassicMilliSeconds--in milliseconds
	 SET @GrandDiffInMilliSeconds=@GrandDiffInMilliSeconds/3600000--in hours
	 SET @HourDiff=floor(@GrandDiffInMilliSeconds)--numeric part in hours
	 IF((@GrandDiffInMilliSeconds-@HourDiff)>0)
	 BEGIN
		 set @MinuteDiff=@GrandDiffInMilliSeconds-@HourDiff--decimal part in hours
		 IF(@MinuteDiff>0)
		 Begin
			SET @MinuteDiff=@MinuteDiff*60---in minutes
			IF((@MinuteDiff-floor(@MinuteDiff))>0)
			BEGIN
				SET @SecondDiff=@MinuteDiff-floor(@MinuteDiff)--decimal part
			END	
				Set @MinuteDiff=floor(@MinuteDiff) -- get minutes
				IF(@SecondDiff>0)
				BEGIN
					SET @SecondDiff=@SecondDiff*60 --get seconds
					IF((@SecondDiff-Floor(@SecondDiff))>0)
					BEGIN
					SET @MilliSecondDiff=@SecondDiff-Floor(@SecondDiff)--
					END
					SET @SecondDiff=floor(@SecondDiff)
				
			    END
		 END
	 END
  END
	 
	 
insert into @Table(HoursDiff ,MinuteDiff,SecondDiff ,MilliSecondDiff)
 values
 (@HourDiff,@MinuteDiff,@SecondDiff,@MilliSecondDiff)
 return

END
Posted
Updated 24-Nov-14 21:49pm
v3
Comments
PIEBALDconsult 24-Nov-14 15:48pm    
1 is the correct answer.

Why not just subtract?

SQL
DECLARE @d1 DATETIME = '2014-11-25 22:59:00.000'
DECLARE @d2 DATETIME = '2014-11-25 23:00:00.000'
SELECT  CAST(@d2-@d1 AS TIME)
 
Share this answer
 
check this, Extending the PIEBALDconsult Answer

SQL
select Convert(Datetime,'2014-11-25 23:00:00.000')-Convert(Datetime,'2014-11-25 22:59:0.000')

select Datepart(HH,Convert(Datetime,'2014-11-25 23:00:00.000')-Convert(Datetime,'2014-11-25 22:59:0.000'))
select Datepart(MI,Convert(Datetime,'2014-11-25 23:00:00.000')-Convert(Datetime,'2014-11-25 22:59:0.000'))
select Datepart(SECOND,Convert(Datetime,'2014-11-25 23:00:00.000')-Convert(Datetime,'2014-11-25 22:59:0.000'))
 
Share this answer
 
thanks a lot guys for the efficient solutions

What about getting the difference of dates only and not the time?
 
Share this answer
 
v2
For date difference try this
SQL
DECLARE @d1 DATETIME = '2014-11-25 22:59:00.000'
DECLARE @d2 DATETIME = '2014-11-26 23:00:00.000'
SELECT  DATEDIFF ( DAY , @d1 , @d2 )

For more information refer this link.
Date Difference Without Time[^]

good luck ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900