I need to create a table where we have one date column. I need to insert the date from yesterday to yesterday-30 days.Table output should look like this
There is one way where I can create a table and enter each date individually.
create table test
(
c_date date
)
insert into test values (trunc(sysdate)-1);
insert into test values (trunc(sysdate)-2);
insert into test values (trunc(sysdate)-3);
insert into test values (trunc(sysdate)-4);
insert into test values (trunc(sysdate)-5);
.....
insert into test values (trunc(sysdate)-30);
what is the efficient way to do it.
CodePudding user response:
You can use a hierarchical query such as
CREATE TABLE test AS
SELECT TRUNC(sysdate) - level AS "date"
FROM dual
CONNECT BY level <= 30
or preferably use directly the query without the first line(CTAS) as a subquery for your main query