[英]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
不是extract()
函数的有效字段。 您需要使用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周号,可以通过'IW'
日期格式元素而不是'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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.