开发者

Representing dates which are a certain number of days before or after another date in a RDBMS

Is there a standard way to represent dates which are a certain number of days before or after another date in an RDBMS?

For example, let's say Date 1 is 30th October, 2005, which can obviously be stored in a datetime column or similar in the RDB.

But let's say Date 2 is "3 days After Date 1", how would you represent it in the RDB, assuming that Date 1 can be changed anytime in the future, which 开发者_运维问答means Date 2 would have to be updated automatically somehow?

Thanks!!


Give 2 items of information, you can always work out the third

  • Start Date + Offset = Another Date
  • Another Date - Start Date = Offset

In this case, you appear to have 2 known facts

  • Start date
  • Offset

Store these and make "Another Date" a computed column with DATEADD

CREATE TABLE whatever (
...
StartDate date NOT NULL,
DayOffset smallint NOT NULL,
AnotherDate AS DATEADD (day, DayOffset, StartDate),
...

This way, AnotherDate will be maintained by the database engine as the 2 input values changes

If any of the 3 can change then you usually have to use a trigger...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜