Reputation: 113
Not sure what I am missing here.
I am using the following code
DECLARE @sqlText nvarchar(4000)
SET @sqlText = N'SELECT InitialComment, DATEDIFF(d, InitialComment, GETDATE() ) AS Duration FROM dbo.SocialManagementTracker;'
DECLARE @newVal nvarchar(4000)
SET @newVal = ''
exec sp_executesql @sqlText, @newVal out
UPDATE dbo.SocialManagementTracker
SET DaysToResolve = @newVal
WHERE SocialID = 2
The dates being compared are 2018/07/08 and 2018/08/31. My result should be 23. Any reason why this returns 0 instead?
Upvotes: 1
Views: 82
Reputation: 46
You can simply the complete query in single update query like below.
UPDATE dbo.SocialManagementTracker
SET DaysToResolve = DATEDIFF(d, InitialComment, GETDATE() )
WHERE SocialID = 2
Upvotes: 1
Reputation: 25112
I don't see any reason for dynamic sql here... this should work fine:
UPDATE dbo.SocialManagementTracker
SET DaysToResolve = DATEDIFF(day, InitialComment, GETDATE() )
WHERE SocialID = 2
Upvotes: 6