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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Seconds to DD:HH:NN:SS fails when negative
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ino mart
Starting Member

12 Posts

Posted - 05/16/2013 :  04:34:56  Show Profile  Reply with Quote
All

I have a table with some fields such as a unique IDNumber (fldIDNumber) and the timestamp on which the record was created (fldSubmitdate).

The target is to calculate the difference between the SubmitDate and an ExpireDate (fldExpireDate). The user gets a prompt to enter "Amount of days to add". In SQL-string below I choose to add 1 day to the submitdate. Furthermore, all calculations must be made in local timezone. My timezone is GMT+1 which also explains why I frequently add 1 hour to the dates. (all times are stored in GMT)

In column fldMinutesBeforeExpire I calculate the difference between the submitdate and the enddate in minutes. The result of this calculation is correct.

In column fldDaysBeforeExpire I try to display the difference in format DAY:HOUR:MINUTES:SECONDS. The calculation is correct when the enddate is still after the submitdate (so in case fldMinutesBeforeExpire is positive). However, when the enddate has already passed, the result of fldDaysBeforeExpire is incorrect.

For example:
field fldSubmitDate = 2013-05-15 08:20:12.000
field fldExipreDate = 2013-05-16 08:20:12.000
CurrentTimeStamp = 2013-05-16 07:17:12.000
Query below returns in -63 for fldMinutesBeforeExpire. This is true.
But it returns 0:22:56:00 as fldDaysBeforeExpire whilst it should be -0:01:03:00

Does someone know what is wrong with my query?

SELECT PERCENT fldIDNumber, DATEADD(hour, 1, fldSubmitDate) AS fldSubmitDate, DATEADD(day, 1, DATEADD(hour, 1, fldSubmitDate)) AS fldExpireDate, 
                      DATEDIFF(N, DATEADD(hour, 1, CURRENT_TIMESTAMP), DATEADD(day, 1, DATEADD(hour, 1, fldSubmitDate))) AS fldMinutesBeforeExpire, CAST(DATEDIFF(s, 
                      DATEADD(hour, 1, CURRENT_TIMESTAMP), DATEADD(day, 1, DATEADD(hour, 1, fldSubmitDate))) / 86400 AS varchar(50)) + ':' + CONVERT(VarChar, DATEADD(S, 
                      DATEDIFF(s, DATEADD(hour, 1, CURRENT_TIMESTAMP), DATEADD(day, 1, DATEADD(hour, 1, fldSubmitDate))), 0), 108) AS fldDaysBeforeExpire
FROM Table

Edited by - ino mart on 05/16/2013 04:36:46

MuMu88
Aged Yak Warrior

547 Posts

Posted - 05/16/2013 :  10:49:10  Show Profile  Reply with Quote
You are using CURRENT_TIMESTAMP in your query which is a reserved term, which returns current date/time;

Try this:


DECLARE @fldSubmitDate DATETIME = '2013-05-15 08:20:12.000';
DECLARE @fldExipreDate DATETIME = '2013-05-16 08:20:12.000';
DECLARE @CurrentTimeStamp DATETIME = '2013-05-16 07:17:12.000';

SELECT CURRENT_TIMESTAMP;

SELECT  DATEADD(hour, 1, @fldSubmitDate) AS fldSubmitDate, 
		DATEADD(day, 1, DATEADD(hour, 1, @fldSubmitDate)) AS fldExpireDate, 
        DATEDIFF(N, DATEADD(hour, 1, @CurrentTimeStamp), DATEADD(day, 1, DATEADD(hour, 1, @fldSubmitDate))) AS fldMinutesBeforeExpire, 
	    CAST(DATEDIFF(s, DATEADD(hour, 1, @CurrentTimeStamp), DATEADD(day, 1, DATEADD(hour, 1, @fldSubmitDate))) / 86400 AS varchar(50)) + ':' + 
			CONVERT(VarChar, DATEADD(S, DATEDIFF(s, DATEADD(hour, 1, @CurrentTimeStamp), 
			DATEADD(day, 1, DATEADD(hour, 1, @fldSubmitDate))), 0), 108) AS fldDaysBeforeExpire;


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