I have to create a trigger on a table T1. This table has 3 fields. PersonID, StartDate and EndDate.When the user enters the StartDate the EndDate should automatically be calculated as 3 months from the StartDate. Any ideas ?? This is what I had, it does not do anything not even an error:
IF UPDATE(EndDate)
BEGIN
UPDATE t1
SET t1.EndDate = DateAdd(mm, +3, convert(datetime, convert(varchar(2), Month(t1.StartDate)) + '/01/' + convert(varchar(4), Year(t1.StartDate))))
FROM t1 INNER JOIN inserted i ON t1.PersonID = i.PersonID AND t1.StartDate = i.StartDate
WHERE DATEPART(hh, i.EndDate) = 0 AND
DATEPART(mi, i.EndDate) = 0 AND
DATEPART(ss, i.EndDate) = 0 AND
DATEPART(ms, i.EndDate) = 0
END
IF UPDATE(EndDate)
BEGIN
UPDATE t1
SET t1.EndDate = DateAdd(mm, +3, convert(datetime, convert(varchar(2), Month(t1.StartDate)) + '/01/' + convert(varchar(4), Year(t1.StartDate))))
FROM t1 INNER JOIN inserted i ON t1.PersonID = i.PersonID AND t1.StartDate = i.StartDate
WHERE DATEPART(hh, i.EndDate) = 0 AND
DATEPART(mi, i.EndDate) = 0 AND
DATEPART(ss, i.EndDate) = 0 AND
DATEPART(ms, i.EndDate) = 0
END