Skip to content

Instantly share code, notes, and snippets.

@luisartola
Created October 8, 2012 12:52
Show Gist options
  • Save luisartola/3852359 to your computer and use it in GitHub Desktop.
Save luisartola/3852359 to your computer and use it in GitHub Desktop.
Oracle dates
//current_date
SELECT TO_CHAR(CURRENT_DATE, 'DD-MON-YYYY HH:MI:SS') FROMDUAL;
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') FROM DUAL;
/*
Day: D, DD, DDTH, DAY
Month: MM, MON
Year; YY,YYYY-RR,RRRR
*/
//add days
SELECT SYSDATE + 1 FROM DUAL;
SELECT SYSDATE - 1 FROM DUAL;
//add months
SELECT add_months(TO_DATE('27-JAN-2007'), 1) FROM DUAL;
//greatest
SELECT GREATEST(datecol1, datecol2, datecol3)
FROM t;
//next_day
SELECT NEXT_DAY(SYSDATE, 'FRI') FROM DUAL;
//to_char ¿?
SELECT TO_CHAR(SYSDATE + INTERVAL '10' MINUTE, 'HH:MI:SS')
FROM DUAL;
//Stringn to date conversion
SELECT TO_DATE(string, mask)
masks:
YEAR,YYYY,YY
MM,MONTH,MON
WW,W,
HH, HH12, HH24
MI
SS,
//select a part of the date
//Trunc (datetime, format)
SELECT TO_CHAR(TRUNC(SYSDATE, 'YEAR'), 'DD-MON-YYYY HH:MI:SS')
FROM DUAL;
comparing
SELECT * FROM t
WHERE TRUNC(datecol) = TRUNC(SYSDATE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment