Question

What is the difference between these 2 lines in SQL Server 2005 Express?

DATEADD(d, 0, DATEDIFF(d, 0, @Today));

and

DATEADD(d, DATEDIFF(d, 0, @Today), 0);

Other than making this statement fail at random times:

DECLARE @DateSrc DATETIME;

-- Chop off the time part:
SET @DateSrc = DATEADD(d, 0, DATEDIFF(d, 0, @Today));

INSERT INTO dbo.SeqNo(MyGUID, TheDay, LastNo)
SELECT @MyGUID, @DateSrc, 0
WHERE NOT EXISTS ( 
  SELECT 1 FROM dbo.SeqNo AS sn
  WHERE sn.MyGUID = @MyGUID AND sn.TheDay = @DateSrc 
  );
Was it helpful?

Solution 3

The problem was a concurrency issue.

The change of the stored procedure has been in production now for a few days and no problems; interesting fix one thinks. Apparently both versions work.

But, I just reviewed the code again that checks for a single instance of the application and it was moved to after this stored procedure call. Arhhgg! Concurrency issue.

Sorry for the trouble and thanks again for your help.

ps. Just one interesting thing... why couldn't I reproduce the concurrency problem within SQL Server Management Studio as per my comment above? That interleaved perfectly with the newer DateAdd format.

OTHER TIPS

They will both produce the same result but the second is the better format because it will work for other intervals (hour, month, year etc) and the first won't.

The syntax is: DATEADD (datepart , number , date )

Line 1: DATEADD(d, 0, DATEDIFF(d, 0, @Today));

Line 2: DATEADD(d, DATEDIFF(d, 0, @Today), 0);

So the answer is that number resolves to an int (line 2) , and date resolves to a datetime (line1) - and that is the difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top