Monday, February 18, 2013

Date Manipulation


Date arithmetic is an important aspect of our day-to-day life. We find the age of a person by subtracting his date of birth from today’s date. We compute the date a warranty expires by adding the warranty period to the purchase date. Drivers’ license expirations, bank interest calculation, and a host of other things all depend on date arithmetic. It is extremely important for any database to support such common date arithmetic operations.


Date Functions:
Function
Use
ADD_MONTHS
Adds months to a date
LAST_DAY
Computes the last day of the month
MONTHS_BETWEEN
Determines the number of months between two dates
NEW_TIME
Translates a time to a new time zone
NEXT_DAY
Returns the date of the next specified weekday
ROUND
Rounds a date/time value to a specified element
SYSDATE
Returns the current date and time
TO_CHAR
Converts dates to strings
TO_DATE
Converts strings and numbers to dates
TRUNC
Truncates a date/time value to a specific element

Addition

Adding two dates doesn’t make sense. However, we can add days, months, years, hours, minutes, and seconds to a date to generate a future date and time. The “+” operator allows us to add numbers to a date. The unit of a number added to a date is assumed to be days. Therefore, to find tomorrow’s date, we can 
add 1 to SYSDATE:



SELECT SYSDATE, SYSDATE+1 FROM DUAL;

No comments:

Post a Comment