Sql-server – Building test site, update dates (back date records)

datetimesql-server-2008update

I have 3 tables that all connect and I need to back date them in an MS SQL 2008 setting.
I was thinking about how to do this and not sure if I am on the correct path. The big problem I have is I have columns that are null, and I want to keep them null if they are. Otherwise I want to adjust the date by 1,2,3 days depending on value….
Here is what I am trying, it adjusts the 2nd and 3rd tables, but the first I assume is failing and I am not understanding why.

    BEGIN TRANSACTION
declare @NumberOfDays int = -1
declare @Subject varchar(70) = 'Daily Nurse Rounding -1'
declare @newSubject varchar(70) = 'Daily Nurse Rounding'
UPDATE hdIssues
SET hdIssues.IssueDate = IsNull(hdIssues.IssueDate,DATEADD(day,@NumberOfDays,hdIssues.IssueDate)),
hdIssues.StartDate = IsNull(hdIssues.StartDate, DATEADD(day,@NumberOfDays,hdIssues.StartDate )),
hdIssues.ResolvedDate = IsNull(hdIssues.ResolvedDate, DATEADD(day,@NumberOfDays,hdIssues.StartDate )),
hdIssues.LastUpdated = IsNull(hdIssues.LastUpdated, DATEADD(day,@NumberOfDays,hdIssues.StartDate )),
hdIssues.DueDate = IsNull(hdIssues.DueDate, DATEADD(day,@NumberOfDays,hdIssues.StartDate )),
hdIssues.AcknowledgeDate = IsNull(hdIssues.AcknowledgeDate, DATEADD(day,@NumberOfDays,hdIssues.StartDate ))
Where
hdIssues.Subject = @Subject

UPDATE hdComments 
SET hdComments.CommentDate = DATEADD(day,@NumberOfDays,hdComments.CommentDate)
WHERE hdComments.IssueID in(select hdIssues.IssueID from hdIssues where hdIssues.Subject = @Subject)

UPDATE hdStatusChangeHistory 
SET hdStatusChangeHistory.ModifiedDateTime = DATEADD(day,@NumberOfDays,hdStatusChangeHistory.ModifiedDateTime)
WHERE hdStatusChangeHistory.IssueID in(select hdIssues.IssueID from hdIssues where hdIssues.Subject = @Subject)

UPDATE hdIssues
SET hdIssues.Subject = @newSubject
where hdIssues.Subject = @Subject

Best Answer

I think I got it.....

SET hdIssues.IssueDate =  COALESCE(DATEADD(day,@NumberOfDays,hdIssues.IssueDate),null),
hdIssues.StartDate =  COALESCE( DATEADD(day,@NumberOfDays,hdIssues.StartDate ),null),
hdIssues.ResolvedDate =  COALESCE(DATEADD(day,@NumberOfDays,hdIssues.ResolvedDate ),null),
hdIssues.LastUpdated =  COALESCE( DATEADD(day,@NumberOfDays,hdIssues.LastUpdated ),null),
hdIssues.DueDate =  COALESCE( DATEADD(day,@NumberOfDays,hdIssues.DueDate ),null),
hdIssues.AcknowledgeDate =  COALESCE( DATEADD(day,@NumberOfDays,hdIssues.AcknowledgeDate ),null)

Coalesce seemed to work for me, I had no idea what coalesce was before but saw it in this example and read up on it here

Anyone know of any drawbacks to this approach?