Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
DECLARE @bdate as datetime
SET @bdate = '2005-03-09'
SELECT datediff(mm, @bdate, GetDate()) as AgeInMonths
SELECT datediff(dd, dateadd(mm, datediff(mm, @bdate, GetDate()), @bdate), GetDate()) as AgeInDaysSansMonth
Alter Function DateDifference
(@Date1 as datetime,
@Date2 as datetime)
Returns Varchar(300)
as
begin
declare @Years varchar(100), @mnts varchar(100), @days varchar(100)
Set @years = datediff(yy,@Date1,@date2)
Set @date1 = dateadd(yy,cast(@years as int),@Date1)
set @Mnts= datediff(m, @Date1,@date2)
Set @Date1 = Dateadd(m,cast(@mnts as int),@date1)
Set @Days= datediff(d,@Date1,@date2)
Return @Years + ' Years, ' + @mnts + ' Months, ' + @days + ' Days'
End
go
Select dbo.DateDifference('1964-05-09' , getdate())
set dateformat dmy
select datediff(yy, '31/12/2000', '01/01/2001')
select datediff(yy, '01/01/2000', '31/12/2001')
Alter Function DateDifference
(@Date1 as datetime,
@Date2 as datetime)
Returns Varchar(300)
as
begin
declare @Years varchar(100), @mnts varchar(100), @days varchar(100)
Set @years = datediff(yy,@Date1,@date2)
Set @date1 = dateadd(yy,cast(@years as int),@Date1)
set @Mnts= datediff(m, @Date1,@date2)
Set @Date1 = Dateadd(m,cast(@mnts as int),@date1)
Set @Days= datediff(d,@Date1,@date2)
If @mnts < 0
begin
set @years = @years - 1
set @Mnts = 12 + @mnts
end
go
Select dbo.DateDifference('1996-09-5' , getdate())
ALTER Function dbo.CalculateAge
(
@BirthDate As DateTime,
@Now as DateTime
)
Returns VarChar(100)
AS
Begin
--dateformat= yyyymmdd
declare @from datetime
declare @to datetime
set @from = Convert(VarChar(4), Year(@BirthDate))
+ Right('00' + Convert(VarChar(2), Month(@BirthDate)), 2)
+ Right('00' + Convert(VarChar(2), Day(@BirthDate)), 2)
set @to = Convert(VarChar(4), Year(@Now))
+ Right('00' + Convert(VarChar(2), Month(@Now)), 2)
+ Right('00' + Convert(VarChar(2), Day(@Now)), 2)
declare @year int set @year=0
declare @month int set @month=0
declare @day int set @day=0
declare @rndate datetime
set @rndate=@from --rndate = runningdate
--calculate the @year
if(datepart(year,@to)>datepart(year,@from))
begin
set @year = datediff(year,@from,@to)
set @rndate = dateadd(year,@year,@from) --update runningdate
if(@rndate>@to)
begin
set @year = datediff(year,@from,dateadd(year,-1,@to)) --calculate years from @from to @to - 1 year
set @rndate = dateadd(year,@year,@from) --update runningdate
end
end
--add 1 month as long as running date is smaller than or equal to @to
while @rndate<=@to
begin
set @rndate = dateadd(month,1,@rndate)
if (@rndate<=@to)
begin
set @month=@month+1
end
end
--set @rndate back 1 month
set @rndate=dateadd(month,-1,@rndate)
--start to count days
while @rndate<@to
begin
set @rndate=dateadd(day,1,@rndate)
set @day=@day+1
end
Return Convert(VarChar(5), @year) + ' Years, ' + Convert(VarChar(5), @Month) + ' Months, ' + Convert(VarChar(5), @day) + ' Days'
End
ALTER Function GetDonuts
(
@Date1 dateTime,
@Date2 DateTime
)
Returns VarChar(10)
As
Begin
RETURN Convert(VarChar(4), CASE WHEN DateAdd(yy,DateDiff(yy,@Date1,@Date2),@Date1)>@Date2 THEN DateDiff(yy,@Date1,@Date2)-1 ELSE DateDiff(yy,@Date1,@Date2) END)
+ ',' +
Convert(VarChar(2), CASE WHEN DateAdd(mm,DateDiff(mm,@Date1,@Date2),@Date1)>@Date2 THEN (DateDiff(mm,@Date1,@Date2)-1)%12 ELSE DateDiff(mm,@Date1,@Date2)%12 END)
+ ',' +
Convert(VarChar(2), DateDiff(dd,DateAdd(mm,CASE WHEN DateAdd(mm,DateDiff(mm,@Date1,@Date2),@Date1)>@Date2 THEN (DateDiff(mm,@Date1,@Date2)-1)%12 ELSE DateDiff(mm,@Date1,@Date2)%12 END ,DateAdd(yy,CASE WHEN DateAdd(yy,DateDiff(yy,@Date1,@Date2),@Date1)>@Date2 THEN DateDiff(yy,@Date1,@Date2)-1 ELSE DateDiff(yy,@Date1,@Date2)END,@Date1)) ,@Date2))
End
Alter Function DateDifference
(@Date1 as datetime,
@Date2 as datetime)
Returns Varchar(300)
as
begin
declare @Years varchar(100), @mnts varchar(100), @days varchar(100)
Set @years = datediff(yy,@Date1,@date2)
Set @date1 = dateadd(yy,cast(@years as int),@Date1)
set @Mnts= datediff(m, @Date1,@date2)
Set @Date1 = Dateadd(m,cast(@mnts as int),@date1)
Set @Days= datediff(d,@Date1,@date2)
If @mnts < 0
begin
set @years = @years - 1
set @Mnts = 12 + @mnts
end
if @Days < 0
begin
set @mnts = @mnts -1
-- how many fng days were there this during the mnt in question?
Declare @iDays int, @month int
set @month = month(@date2)
declare @d1 datetime
declare @d2 datetime
set @d1 =cast(year(@date2)as varchar(4)) + '/' + cast(month(@date2) as varchar(2)) + '/1'
set @d2 = cast(year(@date2)as varchar(4)) + '/' + cast(month(@date2)+1 as varchar(2)) + '/1'
set @days = datediff(d,@d1 ,@d2) + @days
end
Return @Years + ' Years, ' + @mnts + ' Months, ' + @days + ' Days'
DECLARE @bdate as datetime
SET @bdate = '03/09/2005'
SELECT datediff(yy, @bdate, GetDate()) as Years
SELECT datediff(mm, @bdate, GetDate()) % 12 as Months
SELECT datediff(dd, dateadd(mm, datediff(mm, @bdate, GetDate()), @bdate), GetDate()) as Days