Tasneema
Tasneema

Reputation: 61

Difference in dates of two columns and automatically updating it in third column

Consider there are two columns named StartDate,EndDate(2017/03/09 11:25:02, 2018/08/10 15:20:03) in sql.

I need to get the DurationDate column(number of years, months, days, hours, minutes, seconds) should be automatically updated in the duration column based on StartDate and EndDate in the sql table.

Upvotes: 1

Views: 44

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Since Oracle 11g, you can use generated columns:

alter table t add duration as (EndDate - StartDate);

This will produce an interval result if the two "dates" are stored as timestamps. The difference of two dates is a number of decimal days.

In earlier versions, you need to use views to accomplish the same thing.

Upvotes: 3

Related Questions