--function 호출
SELECT F_WEEKCNT( '20100314' ) AS WEEKCNT FROM DUAL
--function 생성
CREATE OR REPLACE FUNCTION KCOP.F_WEEKCNT (DATEOF IN VARCHAR2)
RETURN VARCHAR2
AS V_WEEKRTN VARCHAR2(8);
v_day VARCHAR2(2) := SUBSTR( DATEOF, 7, 2);
v_yyyymm VARCHAR2(6) := SUBSTR( DATEOF, 1, 6);
BEGIN
IF DATEOF IS NOT NULL THEN
SELECT ( v_day
+ ( 7 - TO_CHAR( TO_DATE( v_yyyymm||v_day, 'YYYYMMDD' ), 'D' ) )
+ ( TO_CHAR( TO_DATE( v_yyyymm||'01', 'YYYYMMDD' ), 'D' ) - 1 ) ) / 7 As Week_Cnt
INTO V_WEEKRTN
From Dual;
END IF;
RETURN V_WEEKRTN;
END F_WEEKCNT;
/
--월단위로 주차를 따졌을 경우에 연기준의 주차는 겹치게 된다. 그래서 마지막 주차를 전주차와 합쳐버렸다.. 뭔가 좀 문제가 있다..
CREATE OR REPLACE FUNCTION KCOP.F_WEEKCNT (DATEOF IN VARCHAR2)
RETURN VARCHAR2
AS V_WEEKRTN VARCHAR2(8);
v_day VARCHAR2(2) := SUBSTR( DATEOF, 7, 2);
v_yyyymm VARCHAR2(6) := SUBSTR( DATEOF, 1, 6);
BEGIN
IF DATEOF IS NOT NULL THEN
SELECT
CASE WHEN LastDay <> 7 AND Week_Cnt = LastWeek THEN Week_Cnt-1
ELSE TO_NUMBER(LastDay)
END Week_Cnt
INTO V_WEEKRTN
FROM (
SELECT
( v_day
+ ( 7 - TO_CHAR( TO_DATE( v_yyyymm||v_day, 'YYYYMMDD' ), 'D' ) )
+ ( TO_CHAR( TO_DATE( v_yyyymm||'01', 'YYYYMMDD' ), 'D' ) - 1 ) ) / 7 Week_Cnt,
( TO_CHAR(LAST_DAY(TO_DATE( v_yyyymm||v_day, 'YYYYMMDD' )),'dd')
+ ( 7 - TO_CHAR( LAST_DAY(TO_DATE( '20100328', 'YYYYMMDD' )), 'D' ) )
+ ( TO_CHAR( TO_DATE( v_yyyymm||'01', 'YYYYMMDD' ), 'D' ) - 1 ) ) / 7 LastWeek,
TO_CHAR( LAST_DAY(TO_DATE( v_yyyymm||'01', 'YYYYMMDD' )), 'D' ) LastDay
From Dual
);
END IF;
RETURN V_WEEKRTN;
END F_WEEKCNT;
/
댓글 없음:
댓글 쓰기