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

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Seconds to DD:HH:NN:SS fails when negative

Author  Topic 

ino mart
Starting Member

12 Posts

Posted - 2013-05-16 : 04:34:56
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

MuMu88
Aged Yak Warrior

549 Posts

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

Try this:
[CODE]

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;


[/CODE]
Go to Top of Page
   

- Advertisement -