Search code examples
sqldatabaseoracle-databasedatecreate-table

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)
)

Solution

  • 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