The most difficult part when working with dates is to be sure that the format of the date you are trying to insert, matches the format of the date column in the database. As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets complicated.
Date functions operate on values of the DATE datatype. All date functions return a value of DATE datatype, except the MONTHS_BETWEEN function, which returns a number.
ADD_MONTHS (dt, i): Returns the date dt plus i months. If i is a decimal number. Oracle will automatically convert it to an integer by truncating the decimal portion (it may also be negative).
CURRENT _DATE: Returns the current date in the Gregorian calendar for the session’s time zone. It requires no arguments.
CURRENT_TIMESTAMP ([p]) Returns the current date and time in the session’s time zone to p digits of the precision, p should be an integer 0 through 9 and defaults to 6. The return datatype is TIMESTAMP WITH TIME ZONE.
LAST_DAY(dt) Returns the last day of the month for the date dt.
MONTHS_BETWEEN (dt1, dt2): Returns the number of months that d11 is later than dt2. A whole number is returned if dt1 and dt2 are the same day of the month or if both are the last day of the month.
NEXT_DAY(dt, s): Returns the date that corresponds to the next day of week specified by the string s following the date dt. The time portion of the date is the same as the time portion of dt.
ROUND(dt[, fint]) : Returns the date dt rounded to the granularity specified in the date-format string fmt.
SYSDATE Returns the current date/time, takes no arguments.
TRUNC(dt[, fmt]) Returns the date dt truncated to the granularity specified by the format string fmt.
1. SYSDATE: the SYSDATE function returns the current system date and time on your local database. The default format that the Oracle database uses is: DD-Mon-YY This is how SQL*Plus will show you the data, when requested.
Syntax: SQL> SELECT SYSDATE FROM dual; SQL> SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24: MI: SS')"NOW" FROM DUAL;
2. CURRENT_DATE: This Function Returns the current date in the Gregorian calendar for the session’s time zone. It requires no arguments.
SQL> SELECT CURRENT_DATE FROM DUAL;
3. CURRENT_TIMESTAMP ([p]): Returns the current date and time in the session’s time zone to p digits of the precision, p should be an integer 0 through 9 and defaults to 6. The return datatype is TIMESTAMP WITH TIME ZONE.
SQL> SELECT CURRENT_TIMESTAMP FROM DUAL;
4. ADD_MONTHS: This Function Calculates difference between two dates.
Syntax: ADD_MONTHS (d,n)
This function Returns the date d plus n months. The argument n can be any integer. If d is the last day of the month or if the resulting month has fewer days than the day component of d, then the result is the last day of the resulting month. Otherwise, the result has the same day component as d.
SQL > SELECT SYSDATE,ADD_MONTHS(SYSDATE,6), ADD_MONTHS(SYSDATE, -6) FROM DUAL; SQL> SELECT TO_CHAR(ADD_MONTHS(hiredate,1),'DD-MON-YYYY') "Next Month" From emp Where ename = 'SMITH';
5. LAST_DAY: This Function Returns the date of the last day of the month. You might use this function to determine how many days are left in the current month.
SQL> SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE)–SYSDATE "Days Left" FROM DUAL;
6. MONTHS_BETWEEN: This function returns number of months between two dates d1 and d2. If d1 later than d2, result is positive; if earlier, negative. If d1 and d2 are either the same days of the month or both last days of months, the result is always an integer; otherwise oracle calculates the fractional portion of the result based on a 31-day month and considers the difference in components of d1 and d2.
SQL> SELECT MONTHS_BETWEEN (TO_DATE(’02-02-1995’,’MM-DD-YYYY’), TO_DATE(’01-01-1995’,’MM-DD-YYYY’)) "Months" FROM DUAL;
7. NEXT_DAY: This Function returns the date of the first weekday named by char that is later than the d. The argument char must be a day of the week in your session’s date language-either the full name or the abbreviation.
SQL> SELECT NEXT_DAY('1-JUNE-09','TUESDAY') "NEXT DAY" FROM DUAL;
8. ROUND: The SQL ROUND() function rounds a number to a specific length or precision.
Syntax ROUND (d [, fmt])
• It Returns the date dt rounded to the granularity specified in the date-format string fmt.
• If the format you specify is ‘MON’ and if date is Less than or to <= 15 then it will the day’s date a given month.
• If the day you specify is greater than 15 it display date of first day of next month.
Example1: SQL> SELECT SYSDATE, HIREDATE, ROUND (HIREDATE,'MON') FROM EMP WHERE SAL > 3000; Example2: SQL> SELECT ROUND (TO_DATE ('2-OCT-92'), 'MON') "NEW YEAR" FROM DUAL;