Sunday, September 14, 2008

Oracle Extract function

Here is a function I ran across in Oracle that I consider very helpful. I used to use the to_char function to get the piece or pieces of a date I needed to display or for calculations or what have you. Now I simply use the EXTRACT function to, well, extract the piece of the date I need.

If I needed the year, I would simply issue the following:

  1. select extract(YEAR from sysdate) from dual;  


Or if I needed the month:

  1. select extract(MONTH from sysdate) from dual;  


This function works with dates, timestamps and even the interval datatype.

No comments: