使用 sql 从另一个数据库中提取日、周和月

Extracting day, week and month from another database using sql

我有一个数据库(不是我的),我需要从中提取日期并将其放入另一个数据库(我的),并为日、周和月设置单独的列。在原始数据库中,它看起来像 05-APR-10,但我无法让它出现在单独的列中。

这是我目前所做的:

CREATE TABLE Seasontime (
Seasontime number(2) PRIMARY KEY,
Month number(2) NOT NULL,
Week number(2) NOT NULL,
Day number(2) NOT NULL);

CREATE SEQUENCE Seasontime_Seq
START WITH  0
INCREMENT BY 1
MINVALUE 0
NOCACHE
NOCYCLE;

insert into Seasontime
    select Seasontime_seq.nextval,
           Month,
           Week,
           Day
    from   (
            select extract(Month from PDate) as Month,
                   extract(Week from PDate) as Week,
                   extract(Day from PDate) as Day
            From notmydatabase.performance
           );

最后一点不起作用所以如果有人能帮助我那就太好了 提前致谢

WEEK 不是 the extract() function 的有效字段。您需要使用 to_char() 来获取周数:

select Seasontime_seq.nextval,
       Month,
       Week,
       Day
from   (
        select extract(Month from PDate) as Month,
               to_number(to_char(PDate, 'WW')) as Week,
               extract(Day from PDate) as Day
        From notmydatabase.performance
       );

为了保持一致性,您可能更愿意对其他字段使用 to_char() 而不是 extract()。您可能还需要 ISO 周数,您可以使用 the 'IW' date format element 而不是 'WW'.

获得

或者,如果您想要一个月中的第几周而不是一年中的第几周,则只需 'W';但这可能表明您也想要星期几,而不是 extract() 给您的月份中的某一天。

您不能使用 extract 从日期列中获取周数。尝试使用 to_char 代替:

insert into Seasontime
select Seasontime_seq.nextval,
       Month,
       Week,
       Day
from   (
        select to_char(PDate, 'mm') as Month,
               to_char(PDate, 'WW) as Week,
               to_char(PDate, 'dd') as Day
        From notmydatabase.performance
       );