DECLARE
M_DATE1 DATE;
M_VARCHAR2 VARCHAR2(30) := '5';
M_INTO1 VARCHAR2(50);
M_INTO2 VARCHAR2(50);
M_INTO3 VARCHAR2(50);
M_INTO4 VARCHAR2(50);
M_INTO5 VARCHAR2(50);
--
M_INTO6 VARCHAR2(50);
M_INTO7 VARCHAR2(50);
M_INTO8 VARCHAR2(50);
BEGIN
SELECT ADD_MONTHS(DATE '2015-01-01', -2) INTO M_DATE1 FROM DUAL;
DBMS_OUTPUT.put_line(TO_CHAR(M_DATE1, 'YYYY-MM-DD'));
--SELECT ADD_MONTHS((M_DATE1 + M_VARCHAR2 - 1), -1) INTO M_INTO1 FROM DUAL;
SELECT TO_CHAR(ADD_MONTHS((M_DATE1 + M_VARCHAR2 - 1), -1), 'YYYY-MM-DD')
INTO M_INTO1
FROM DUAL;
DBMS_OUTPUT.put_line(M_INTO1);
IF TO_NUMBER(TO_CHAR(SYSDATE, 'DD')) = 19 THEN
DBMS_OUTPUT.put_line('1号');
END IF;
SELECT TO_CHAR(SYSDATE - 5, 'YYYY-MM-DD HH24:MM:SS')
INTO M_INTO2
FROM DUAL;
SELECT TO_DATE('2014-06-10 12:05:22', 'YYYY-MM-DD HH24:MI:SS')
INTO M_INTO3
FROM DUAL;
/* SELECT TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM') FIRST_DAY LAST_DAY(ADD_MONTHS(SYSDATE, -1)) LAST_DAY
FROM DUAL;*/
-- SELECT TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM') INTO M_INTO4 FROM DUAL;
-- SELECT LAST_DAY(ADD_MONTHS(SYSDATE, -1)) LAST_DAY INTO M_INTO5 FROM DUAL;
--上月第一天
SELECT TO_CHAR(TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM'), 'YYYY-MM-DD')
INTO M_INTO4
FROM DUAL;
--上月最后一天
SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)), 'YYYY-MM-DD')
INTO M_INTO5
FROM DUAL;
--本月第一天
SELECT TO_CHAR(TRUNC(ADD_MONTHS(SYSDATE, 0), 'MM'), 'YYYY-MM-DD')
INTO M_INTO4
FROM DUAL;
--上一年的第一天
SELECT TO_CHAR(TRUNC(ADD_MONTHS(SYSDATE, -1), 'YY'), 'YYYY-MM-DD')
INTO M_INTO4
FROM DUAL;
DBMS_OUTPUT.put_line(TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM'));
DBMS_OUTPUT.put_line(M_INTO4);
DBMS_OUTPUT.put_line(M_INTO5);
--加减时间练习
SELECT TO_CHAR(DATE '2015-01-01' - 1, 'YYYY-MM-DD')
INTO M_INTO6
FROM DUAL;
SELECT TO_CHAR(TO_DATE('2015-02-01 15:14:21', 'yyyy-MM-dd HH24:mi:ss') - 1,
'YYYY-MM-DD')
INTO M_INTO7
FROM DUAL;
SELECT TO_CHAR(TO_DATE('2015-02-01 15:14:21', 'yyyy-MM-dd HH24:mi:ss') - 1,
'YYYY-MM-DD HH24:mi:ss')
INTO M_INTO8
FROM DUAL;
END;