2010-09-23 43 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() 
相關問題