I am trying to get the results of three integer values corresponding to "number of Years" "number of Months" "number of Days" between two inputted dates...
The code below does not error out but the computed values come out wrong...The expected results should have been Years=0,Months=0 and Days=1...
I am new at MS SQL Server programming. Please help me correct my code. Thanks
****************************************************
CREATE PROCEDURE dbo.HowLongAgo
@d1 smalldatetime ,
@d2 smalldatetime
AS
declare @tempdate smalldatetime, @Years int,@Months int,@Days int
BEGIN
select @tempdate = @d1
select @Years = DateDiff("yyyy", @tempdate, @d2)
select @tempdate = DateAdd("yyyy", @Years, @tempdate)
select @Months = DateDiff("m", @tempdate, @d2)
select @tempdate = DateAdd("m", @Months, @tempdate)
select @Days = DateDiff("d", @tempdate, @d2)
select @tempdate = DateAdd("d", @Days, @tempdate)
update test_table set dateCol_9 = @Years , dateCol_10 = @Months ,dateCol_11 = @Days
END
GO
********************************************
exec HowLongAgo '12/31/97','01/01/98'
select * from test_table
The code below does not error out but the computed values come out wrong...The expected results should have been Years=0,Months=0 and Days=1...
I am new at MS SQL Server programming. Please help me correct my code. Thanks
****************************************************
CREATE PROCEDURE dbo.HowLongAgo
@d1 smalldatetime ,
@d2 smalldatetime
AS
declare @tempdate smalldatetime, @Years int,@Months int,@Days int
BEGIN
select @tempdate = @d1
select @Years = DateDiff("yyyy", @tempdate, @d2)
select @tempdate = DateAdd("yyyy", @Years, @tempdate)
select @Months = DateDiff("m", @tempdate, @d2)
select @tempdate = DateAdd("m", @Months, @tempdate)
select @Days = DateDiff("d", @tempdate, @d2)
select @tempdate = DateAdd("d", @Days, @tempdate)
update test_table set dateCol_9 = @Years , dateCol_10 = @Months ,dateCol_11 = @Days
END
GO
********************************************
exec HowLongAgo '12/31/97','01/01/98'
select * from test_table