0% found this document useful (0 votes)
314 views10 pages

Date Functions in SQL

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 10

Oracle Date Functions Version 11.

ent Date

RENT_DATE DATE ECT TO_CHAR(CURRENT_DATE, 'DD-MON-YYYY HH:MI:SS') FROM dual;

ECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') FROM dual; Day Month Year Fill Mode Julian Date D MM YY FM J DD MON YYYY

mats

DDTH RR

DAY RRRR

ND + -

te> + <integer> ECT SYSDATE + 1 FROM dual;

te> - <integer> ECT SYSDATE - 1 FROM dual;

D_MONTHS

A Month To A Date

_MONTHS(<date>, <number of months_integer>

ECT add_months(SYSDATE, 2) FROM dual;

but be aware of what it is doing ECT add_months(TO_DATE('27-JAN-2007'), 1) FROM dual;

ECT add_months(TO_DATE('28-JAN-2007'), 1) FROM dual;

ECT add_months(TO_DATE('29-JAN-2007'), 1) FROM dual;

ECT add_months(TO_DATE('30-JAN-2007'), 1) FROM dual;

ECT add_months(TO_DATE('31-JAN-2007'), 1) FROM dual;

ECT add_months(TO_DATE('01-FEB-2007'), 1) FROM dual;

RRENT_DATE

rns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE

sessiontimezone format a30

ECT sessiontimezone, current_date M dual;

ER SESSION SET TIME_ZONE = '-5:0';

ECT sessiontimezone, current_date M dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT sessiontimezone, current_date M dual;

ER SESSION SET TIME_ZONE = '-7:0';

ECT sessiontimezone, current_date M dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

MP

rns a VARCHAR2 value containing the datatype code, length in bytes, and internal representation of a value

P(<value> [,<return_format>[,<start_position>[,<length>]]])
8 10

mal

16 17 1008 notation with the character set name 1010 1016 1017

decimal

le Characters

mal notation with the character set name

decimal notation with the character set name

e characters with the character set name

drows format a40

ECT DUMP(SYSDATE) DROWS FROM dual;

ECT DUMP(SYSDATE, 8) DROWS FROM dual;

ECT DUMP(SYSDATE, 16) DROWS FROM dual;

EATEST

rn the Latest Date

ST(<date>, <date>, <date>, ...) ATE TABLE t ( ecol1 DATE, ecol2 DATE, ecol3 DATE) FREE 0;

ERT INTO t VALUES (SYSDATE+23, SYSDATE-10, SYSDATE-24); ERT INTO t VALUES (SYSDATE-15, SYSDATE, SYSDATE+15); ERT INTO t VALUES (SYSDATE-7, SYSDATE-18, SYSDATE-9); MIT;

ECT * FROM t;

ECT GREATEST(datecol1, datecol2, datecol3) M t;

ERVAL

val to adjust date-time

ERVAL '<integer>' <unit> ECT TO_CHAR(SYSDATE, 'HH:MI:SS') M dual;

ECT TO_CHAR(SYSDATE + INTERVAL '10' MINUTE, 'HH:MI:SS') M dual;

ECT TO_CHAR(SYSDATE - INTERVAL '10' MINUTE, 'HH:MI:SS') M dual;

ST_DAY

rns The Last Date Of A Month

T_DAY(<date>) ECT * FROM t;

ECT LAST_DAY(datecol1) FROM t;

AST

rn the Earliest Date

ST(<date>, <date>, <date>, ...) ECT * FROM t;

ECT LEAST(datecol1, datecol2, datecol3) FROM t;

NGTH

rns length in characters

GTH(<date>) ECT LENGTH(last_ddl_time) FROM user_objects;

NGTHB

rns length in bytes

GTHB(<date>) ECT LENGTHB(last_ddl_time) FROM user_objects;

e: Additional forms of LENGTH (LENGTHC, LENGTH2, and LENGTH4) are also available.

rn the Latest Date

(<date>) ECT * FROM t;

ECT MAX(datecol1) FROM t;

rn the Earliest Date

(<date>) ECT * FROM t;

ECT MIN(datecol1) FROM t;

NTHS_BETWEEN

rns The Months Separating Two Dates

THS_BETWEEN(<latest_date>, <earliest_date>) ECT MONTHS_BETWEEN(SYSDATE+365, SYSDATE-365) FROM dual;

ECT MONTHS_BETWEEN(SYSDATE-365, SYSDATE+365) FROM dual;

W_TIME

rns the date and time in time zone zone2 when date and time in time zone zone1 are date

ore using this function, you must set the NLS_DATE_FORMAT parameter to display 24-hour

e. ECT NEW_TIME(TO_DATE('11-10-99 01:23:45', -DD-YY HH24:MI:SS'), 'AST', 'PST') "New Date and Time" M dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT NEW_TIME(TO_DATE('11-10-99 01:23:45', -DD-YY HH24:MI:SS'), 'AST', 'PST') "New Date and Time" M dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';


of next specified date following a date

XT_DAY

T_DAY(<date>, <day of the week>)

ions are SUN, MON, TUE, WED, THU, FRI, and SAT ECT NEXT_DAY(SYSDATE, 'FRI') FROM dual;

UND

rns date rounded to the unit specified by the format model. If you omit the format, the date is rounded to the nearest day

ND(<date_value>, <format>) ECT ROUND(TO_DATE('27-OCT-00'),'YEAR') NEW_YEAR M dual;

lled Out Using TO_CHAR

led Demo

DDSP HH24SP MISP MMSP SSSP ECT TO_CHAR(TO_DATE('10:30:18', 'HH24:MI:SS'), 'HH24SP:MISP:SSSP') M dual;

ECT TO_CHAR(TO_DATE('01-JAN-2008', 'DD-MON-YYYY'), 'DDSP-MONTH-YYYYSP') M dual;

ECT TO_CHAR(TO_DATE('01-JAN-2008', 'DD-MM-YYYY'), 'DDSP-MMSP-YYYYSP') M dual;

ECT TO_CHAR(TO_DATE(sal,'J'), 'JSP') M emp;

SDATE

rns the current date and time set for the operating system on which the database resides

DATE ECT SYSDATE FROM dual;

UNC

vert a date to the date at midnight

NC(<date_time>) ATE TABLE t ( ecol DATE);

ERT INTO t (datecol) VALUES (SYSDATE);

ERT INTO t (datecol) VALUES (TRUNC(SYSDATE));

ERT INTO t (datecol) VALUES (TRUNC(SYSDATE, 'HH'));

ERT INTO t (datecol) VALUES (TRUNC(SYSDATE, 'MI'));

MIT;

ECT TO_CHAR(datecol, 'DD-MON-YYYY HH:MI:SS') M t;

ctively remove part of the date information

cial thanks to Dave Hayes for reminding me of this.

NC(<date_time>, '<format>') ECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') M dual;

first day of the month ECT TO_CHAR(TRUNC(SYSDATE, 'MM'), 'DD-MON-YYYY HH:MI:SS') M dual;

ECT TO_CHAR(TRUNC(SYSDATE, 'MON'), 'DD-MON-YYYY HH:MI:SS') M dual;

ECT TO_CHAR(TRUNC(SYSDATE, 'MONTH'), 'DD-MON-YYYY HH:MI:SS') M dual;

first day of the year ECT TO_CHAR(TRUNC(SYSDATE, 'YYYY'), 'DD-MON-YYYY HH:MI:SS') M dual;

ECT TO_CHAR(TRUNC(SYSDATE, 'YEAR'), 'DD-MON-YYYY HH:MI:SS') M dual;

s in WHERE Clause Joins

ECT SYSDATE FROM dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT SYSDATE FROM dual;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

ATE TABLE t ( ecol DATE);

ERT INTO t tecol) UES SDATE);

ECT * FROM t;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT * FROM t;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

ECT * FROM t;

ECT SYSDATE FROM dual;

ECT * FROM t RE datecol = SYSDATE;

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT * FROM t;

ECT SYSDATE FROM dual;

ECT TRUNC(SYSDATE) FROM dual;

ECT * FROM t RE TRUNC(datecol) = TRUNC(SYSDATE);

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

ZE

rns The Number Of Bytes Required By A Value

ZE(e IN DATE) RETURN NUMBER ECT VSIZE(SYSDATE) FROM dual;

e Calculations

rns A Day A Specified Number Of Days In The Future Skipping Weekends

ATE OR REPLACE FUNCTION business_date (start_date DATE, s2Add NUMBER) RETURN DATE IS

unter NATURAL := 0; rDate DATE := start_date; yNum POSITIVE; ipCntr NATURAL := 0; IN HILE Counter < Days2Add LOOP CurDate := CurDate+1; DayNum := TO_CHAR(CurDate, 'D');

IF DayNum BETWEEN 2 AND 6 THEN Counter := Counter + 1; ELSE SkipCntr := SkipCntr + 1; END IF; ND LOOP; ETURN start_date + Counter + SkipCntr; business_date;

ness Date function, above, enhanced by Larry Benton to handle negative values for the days2add parameter.

ATE OR REPLACE FUNCTION business_date (start_date DATE, s2add NUMBER) RETURN DATE IS unter NATURAL := 0; rDate DATE := start_date; yNum POSITIVE; ipCntr NATURAL := 0; rection INTEGER := 1; -- days after start_date sinessDays NUMBER := Days2Add; IN F Days2Add < 0 THEN Direction := - 1; -- days before start_date BusinessDays := (-1) * BusinessDays; ND IF;

HILE Counter < BusinessDays LOOP CurDate := CurDate + Direction; DayNum := TO_CHAR( CurDate, 'D');

IF DayNum BETWEEN 2 AND 6 THEN Counter := Counter + 1; ELSE SkipCntr := SkipCntr + 1; END IF; ND LOOP;

ETURN start_date + (Direction * (Counter + SkipCntr)); business_date;

rns The First Day Of A Month

ATE OR REPLACE FUNCTION fday_ofmonth(value_in DATE)

URN DATE IS o VARCHAR2(2); r VARCHAR2(4); IN Mo := TO_CHAR(value_in, 'MM'); Yr := TO_CHAR(value_in, 'YYYY'); ETURN TO_DATE(vMo || '-01-' || vYr, 'MM-DD-YYYY'); EPTION HEN OTHERS THEN RETURN TO_DATE('01-01-1900', 'MM-DD-YYYY'); fday_ofmonth;

e Calculations

rns The Number Of Seconds Between Two Date-Time Values

ATE OR REPLACE FUNCTION time_diff ( E_1 IN DATE, DATE_2 IN DATE) RETURN NUMBER IS NUMBER; NUMBER; NUMBER(5,0); NUMBER(5,0);

TE_1 TE_2 COND_1 COND_2

IN - Get Julian date number from first date (DATE_1) DATE_1 := TO_NUMBER(TO_CHAR(DATE_1, 'J'));

- Get Julian date number from second date (DATE_2) DATE_2 := TO_NUMBER(TO_CHAR(DATE_2, 'J'));

- Get seconds since midnight from first date (DATE_1) SECOND_1 := TO_NUMBER(TO_CHAR(DATE_1, 'SSSSS'));

- Get seconds since midnight from second date (DATE_2) SECOND_2 := TO_NUMBER(TO_CHAR(DATE_2, 'SSSSS'));

ETURN (((NDATE_2 - NDATE_1) * 86400)+(NSECOND_2 - NSECOND_1)); time_diff;

ulating time from seconds

ed by John K. Hinsdale 0/06 to c.d.o.misc

ECT DECODE(FLOOR(999999/86400), 0, '', FLOOR(999999/86400) || ' day(s), ') || TO_CHAR(TO_DATE(MOD(999999, 86400),'SSSSS'), 'HH24:MI:SS') AS elapsed M dual;

in counts per ten minute increment

ed by Michele Cadot

9/08 to c.d.o.misc

ER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

ECT TRUNC(SYSDATE) + dbms_random.value(0,86400)/86400 M dual NECT BY LEVEL <= 10;

H data AS ( ELECT TRUNC(SYSDATE)+dbms_random.value(0,86400)/86400 h ROM dual ONNECT BY LEVEL <= 10) ECT TO_CHAR(h,'DD/MM/YYYY HH24:MI:SS') h, TO_CHAR(TRUNC(h) TRUNC(TO_CHAR(h,'SSSSS')/600)/144, 'DD/MM/YYYY HH24:MI:SS') "10m" M data ER BY h;

You might also like