How to get the number of months and days from two dates

I have two tables:

AgeMilestones //Represents available timespans
Id int     
Description varchar //(newborn, 1 month old, 2 month old, etc.)
NbrMonths int //( 0, 1, 2, etc.)
NbrDays int //(0, 1, 2, etc.)   


Child
Id int     
DateOfBirth DateTime  

I need to get AgeMilestones so that this child is currently aged. The problem is that a real month can have 28 days, 30 days, or 31 days. Therefore, if I convert NbrMonths to days, sometimes I can disconnect for a few days.

Is there any other way to do this, which would be more accurate using the existing table structure?

EDIT:
I need to understand which agemilesstone corresponds to the number of months / days that exist at the time of the birth of the baby today (something similar below). I get worked out in cases where a century-old milestone can be 3 months and 15 days, or 5 months and 7 days ...

SET @Days = DateDiff(d,child.DateOfBirth, GetDate())  
SET @Months = DateDiff(m,child.DateOfBirth, GetDate()) 

SELECT * FROM AgeMileStone WHERE NbrMonths < @Months AND NbrDays < @Days 


,
AgeMilestone:
Id: 4
: "5 1/2 "
: 5
: 15

+3
4

, , DATEADD , , :

declare @AgeMilestones table (
    NbrMonths int not null,
    NbrDays int not null,
    [Description] varchar(64) not null
)

declare @Child table (
    ChildId int not null identity,
    Name varchar(32) not null,
    DateOfBirth datetime not null
)

insert @AgeMilestones values (5, 15, '5 and 1/2 months')
insert @AgeMilestones values (0, 0, 'newborn')

insert @Child values ( 'Yearling', '2010-01-01' )
insert @Child values ( 'Newborn', GETDATE() )

declare @currentChild int = 2

select
    m.*
from @Child c
inner join @AgeMilestones m
    on dateadd(month, m.NbrMonths, dateadd(day, m.NbrDays, c.DateOfBirth)) <= getdate()
where c.ChildId = @currentChild
+1

, datediff(month, DOB, getdate()).

- :

declare @dob datetime = getdate() - 123; --born 123 days ago

select cast(datediff(month, @dob, getdate()) as varchar) + ' month old'
,cast(datediff(day, @dob, getdate()) as varchar) + ' days old'

declare @dob datetime;
set @dob = getdate() - 125;

select 
datediff(month, @dob, getdate()) [Months], 
datediff(day, dateadd(month, datediff(month, @dob, getdate()), @dob), getdate()) [Offset Days]
+2

- :

DATEPART(Month, NOW())
DATEPART(DAY, NOW())

SELECT datepart (dayofyear, c.DateOfBirth) "doy" c

+1

, AgeMilestones DATEADD, , .

-- setup the AgeMilestone table with some initial data
CREATE table AgeMilestone (milestone_month int, milestone_name varchar(50))
insert into AgeMilestone (milestone_month, milestone_name) values (1, '1 month')
insert into AgeMilestone (milestone_month, milestone_name) values (2, '2 month')
insert into AgeMilestone (milestone_month, milestone_name) values (3, '3 month')
insert into AgeMilestone (milestone_month, milestone_name) values (4, '4 month')
...
insert into AgeMilestone (milestone_month, milestone_name) values (12, '12 month')
insert into AgeMilestone (milestone_month, milestone_name) values (24, '24 month')

Declare @DOB DATETIME = '1/14/2009'
SELECT 
     milestone_month, milestone_name
FROM AgeMilestone
where DATEADD(month, milestone_month, @DOB) <= GETDATE()
+1

Source: https://habr.com/ru/post/1766266/


All Articles