thursdaysgeek
thursdaysgeek

Reputation: 7946

Oracle define Day, Month in table definition

In SQLServer, in a table definition, I can have the following:

CREATE TABLE [DateTable] ( 
    [Daily_Date] [date] null,
    [DayPart] as (datepart(day,[Daily_Date])),
    [MonthPart] as (datepart(month,[Daily_Date]))
)

Is it possible to do the same in Oracle? The following is not making it happy:

CREATE TABLE DateTable ( 
    Daily_Date Date null,
    DayPart as Extract (Day from Daily_Date)
    MonthPart Extract (Month from Daily_Date)
)

Upvotes: 0

Views: 49

Answers (1)

GMB
GMB

Reputation: 222582

Your DDL raises error:

ORA-02000: missing ( keyword

Parentheses are mandatory around the computation expression. The as keyword is also mandatory. Finally, you were missing a comma between the declaration of the second and third column.

Consider:

CREATE TABLE DateTable ( 
    Daily_Date Date null,
    DayPart as (Extract(Day from Daily_Date)),
    MonthPart as (Extract(Month from Daily_Date))
);

Demo on DB Fiddle

Upvotes: 2

Related Questions