2010-09-23 112 views
3

我有两个表:如何获取月日数从两个日期

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 

我需要得到一个给定的孩子有目前年龄通过AgeMilestones。问题是,一个月可能有28天,30天或31天。因此,如果我将NbrMonths转换成几天,我可能偶尔会关闭几天。

是否有任何其他方式来做到这一点,使用现有的表结构会更准确?

编辑:
我要图什么agemilesstone对应的月份数/存在于孩子之间的时间日出生,今天(类似下面的东西)。我在情况下,一个时代的里程碑可能是3个月,15天,或5个月和7天得到绊倒了......

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

+0

不要忘了,一个月能有2900天太。不是很经常,但它发生。我听说那天也有婴儿出生,但我没有证实这一点。 – 2010-09-23 18:04:21

+0

你已经知道了,我没有得到你更新的问题。 – 2010-09-23 18:04:39

+0

我想我知道你在说什么......你的意思是说,NbrDays实际上是抵消日,而不是自DOB以来的总天数,对吗? – 2010-09-23 18:06:29

回答

1

我相信这能解决它,因为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 
+0

我想你应该在添加几个月后添加几天,而不是相反。 – 2010-09-23 18:45:15

+0

我不确定这会影响 – Dave 2010-09-23 18:46:03

+0

...因为在本月中旬出现DOB,我想你可以将DOB截断到本月的第一个月,然后添加月份然后添加DOB天和NbrDays – Dave 2010-09-23 18:54:20

2

使用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] 
+0

感谢您的回答。我可以使用datediff得到从出生日期算起的天数和月数,但我需要找到相应的年龄里程石。我编辑了我的原始帖子,试图更好地了解我正在尝试做什么。 – AGoodDisplayName 2010-09-23 18:08:08

+0

+1再次感谢,这可以让我最终达到我想要的位置,但@戴夫的解决方案实际上正是我所需要的。 – AGoodDisplayName 2010-09-23 19:48:44

1

我会建议使用这样的事情:

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

尝试使用

SELECT DATEPART(DAYOFYEAR,c.DateOfBirth)为“DOY”从Child 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() 
相关问题