How can I get all the Days of the previous开发者_运维问答 month in PL/SQL ?
Thanks.
SELECT LAST_DAY(ADD_MONTHS(SYSDATE, -2)) + level
FROM dual
CONNECT BY
level <= LAST_DAY(ADD_MONTHS(SYSDATE, -1)) - LAST_DAY(ADD_MONTHS(SYSDATE, -2))
Like this ?
begin
for i in 1..to_char(trunc(sysdate,'mm')-1,'dd') loop
dbms_output.put_line(i);
end loop;
end;
/
Or if you want date :
begin
for i in 1..to_char(trunc(sysdate,'mm')-1,'dd') loop
dbms_output.put_line(i+trunc(trunc(sysdate,'mm')-1,'mm')-1);
end loop;
end;
/
Try this one
WITH d AS
(
SELECT TRUNC ( sysdate, 'MM' ) - 1 AS dt
FROM dual
)
SELECT dt + LEVEL
FROM d
CONNECT BY LEVEL <= ADD_MONTHS (dt, 1) - dt
精彩评论