create function [dbo].[DateDiffBetweenTwoDates](@Date1 datetime, @Date2 datetime)
RETURNS varchar(100)
AS
Begin
DECLARE @thisYearBirthDay datetime
DECLARE @years int, @months int, @days int
set @thisYearBirthDay = DATEADD(year, DATEDIFF(year, @Date1, @Date2), @Date1)
set @years = DATEDIFF(year, @Date1, @Date2) - (CASE WHEN @thisYearBirthDay > @Date2 THEN 1 ELSE 0 END)
set @months = MONTH(@Date2 - @thisYearBirthDay) - 1
set @days = DAY(@Date2 - @thisYearBirthDay) - 1
return cast(@years as varchar(2)) + ' years,' + cast(@months as varchar(2)) + ' months,' + cast(@days as varchar(3)) + ' days'
end
--Select dbo.DateDiffBetweenTwoDates('1986-01-28', getdate())
RETURNS varchar(100)
AS
Begin
DECLARE @thisYearBirthDay datetime
DECLARE @years int, @months int, @days int
set @thisYearBirthDay = DATEADD(year, DATEDIFF(year, @Date1, @Date2), @Date1)
set @years = DATEDIFF(year, @Date1, @Date2) - (CASE WHEN @thisYearBirthDay > @Date2 THEN 1 ELSE 0 END)
set @months = MONTH(@Date2 - @thisYearBirthDay) - 1
set @days = DAY(@Date2 - @thisYearBirthDay) - 1
return cast(@years as varchar(2)) + ' years,' + cast(@months as varchar(2)) + ' months,' + cast(@days as varchar(3)) + ' days'
end
--Select dbo.DateDiffBetweenTwoDates('1986-01-28', getdate())
10:38 PM |
Category: |
0
comments
Comments (0)