Tuesday, June 20, 2006

Cari lokasi field di dalam Oracle

SELECT DISTINCT TABLE_NAME
FROM ALL_CONS_COLUMNS
WHERE COLUMN_NAME = '77th'

Monday, June 19, 2006

Kalkulasi Tahun dan Bulan di Oracle

/*********************************************************************/
/* Purpose : calculate Years, Month , day
/* Create : setiawanthea
/* use : SELECT DATE_CALCULATE ( end_dt, start_dt ) FROM dual ;
/* Return (0, 1, 16)
/* date : 14/06/2006
/*********************************************************************/
FUNCTION DATE_CALCULATE (datefrom_in IN DATE, dateto_in IN DATE)
RETURN VARCHAR2
AS
DateFrom DATE ;
DateTo DATE ;
nYears NUMBER ;
nMonths NUMBER ;
nDays NUMBER ;
SIGN VARCHAR2(1) := '' ;
BEGIN

-- determine the sign of the difference
IF DateFrom_in <= DateTo_in THEN
DateFrom := DateFrom_in ;
DateTo := DateTo_in ;
ELSE
DateTo := DateFrom_in ;
DateFrom := DateTo_in ;
SIGN := '-' ;
END IF ;

-- calculate the number of months between the two dates
nMonths := TRUNC(MONTHS_BETWEEN(DateTo, DateFrom)) ;

-- calculate the number of days
nDays := DateTo - ADD_MONTHS(DateFrom, nMonths) ;

-- if nDays is negative, arrangement of nMonths
IF nDays < 0 THEN
nMonths := nMonths - 1 ;
nDays := DateTo - ADD_MONTHS(DateFrom, nMonths) ;
END IF ;

-- calculate the number of months passed
nYears := TRUNC(nMonths/12) ;

-- strips the years from the number of months
nMonths := nMonths - nYears * 12 ;

-- return the result
RETURN SIGN || nYears || '- ' || nMonths || '/ ' || nDays ;

EXCEPTION
WHEN OTHERS THEN
RETURN 'Error: ' || SQLERRM ;

END;