在這裏你可以存儲每個日期部分,仍然限制它們的有效日期:
CREATE Table PartialDates
(
YearPart smallint --(2 bytes -32k to +32k)
,MonthPart tinyint --(1 byte 0 to 255)
,DayPart tinyint --(1 byte 0 to 255)
,CompleteDate AS (CONVERT(datetime,CONVERT(varchar(10),YearPart)+'-'+CONVERT(varchar(10),MonthPart)+'-'+CONVERT(varchar(10),coalesce(DayPart,1))))
)
ALTER TABLE dbo.PartialDates ADD CONSTRAINT
CK_PartialDates_IsDate CHECK (ISDATE(CONVERT(varchar(10),YearPart)+'-'+CONVERT(varchar(10),MonthPart)+'-'+CONVERT(varchar(10),coalesce(DayPart,1)))=1)
GO
insert into PartialDates (yearpart,monthpart,daypart) values(2009,91,1) --error
insert into PartialDates (yearpart,monthpart,daypart) values(2009,1,1) --ok
insert into PartialDates (yearpart,monthpart,daypart) values(2009,1,51) --error
insert into PartialDates (yearpart,monthpart,daypart) values(2009,2,29) --error
insert into PartialDates (yearpart,monthpart,daypart) values(2009,2,28) --ok
insert into PartialDates (yearpart,monthpart,daypart) values(2009,2,null)--ok
select * from PartialDates
YearPart MonthPart DayPart CompleteDate
-------- --------- ------- -----------------------
2009 1 1 2009-01-01 00:00:00.000
2009 2 28 2009-02-28 00:00:00.000
2009 2 NULL 2009-02-01 00:00:00.000
(3 row(s) affected
你爲什麼不知道完整的日期?沒有完整日期的日期字段有什麼用? – HLGEM 2009-09-01 13:15:44
有時日期的某一天是真正未知的。例如,我可以填寫稀有未公開的音樂演示記錄,但該記錄可能是錯誤的或部分日期不記錄當天的記錄。所以我想保留延期填寫記錄信息的可能性 – 2009-09-01 13:34:56