Hi Sarah,
If you’re working with a MS SQL DB, then you can use this fucntion for doing the calculation:
CREATE FUNCTION [dbo].[GetNumberOfDaysBetween2Dates]
(
– the format of the dates must be dd/mm/yyyy use convert(date,TheDateToTransform,103) for this
@TheDateB date,
@TheDateA date
)
RETURNS int
AS
BEGIN
– Declare the return variable here
DECLARE @Difference int
SET @Difference = DATEDIFF(day,CAST(@TheDateB AS NVARCHAR(10)),CAST(CONVERT(date,@TheDateA,103) AS NVARCHAR(10)))
-- Return the days between @TheDateB and @TheDateA
RETURN @Difference
END
For having the number of months just replace “day” by “month” as first argument of the DATEDIFF, and if you want to have the number of years just replace “day” by “year”.
For passing the arguments in the correct format just use convert(date,TheDateToTransform,103), it works if the date is in a string format or a datetime format.
I hope this helps,
Olivier