DB

시간 단위 만들어 내는 SQL

Lawmin 2016. 3. 24. 18:32

가끔 필요해서...

-- ORACLE

SELECT  DECODE(LENGTH(:staDt),

            4, TO_CHAR(TO_NUMBER(:staDt) + LEVEL - 1),

            6, TO_CHAR(ADD_MONTHS(TO_DATE(:staDt, 'YYYYMM'), LEVEL - 1), 'YYYYMM'),

            8, TO_CHAR(TO_DATE(:staDt, 'YYYYMMDD') + LEVEL - 1, 'YYYYMMDD')) dt

FROM    DUAL

CONNECT BY LEVEL <=

    DECODE(LENGTH(:staDt),

            4, TO_NUMBER(:endDt) - TO_NUMBER(:staDt) + 1,

            6, MONTHS_BETWEEN(TO_DATE(:endDt, 'YYYYMM'), TO_DATE(:staDt, 'YYYYMM')) + 1,

            8, TO_DATE(:endDt, 'YYYYMMDD') - TO_DATE(:staDt, 'YYYYMMDD') + 1)