15.01.2013 Views

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

182 Part II: SQL and SQL *Plus<br />

29-FEB-04<br />

31-MAR-04<br />

30-APR-04<br />

31-MAY-04<br />

30-JUN-04<br />

31-JUL-04<br />

31-AUG-04<br />

30-SEP-04<br />

31-OCT-04<br />

30-NOV-04<br />

31-DEC-04<br />

MONTHS_BETWEEN Two Dates<br />

You recently came across a file containing the birthdates of a group of friends. You load the<br />

information into a table called BIRTHDAY and display it:<br />

select FirstName, LastName, BirthDate from BIRTHDAY;<br />

FIRSTNAME LASTNAME BIRTHDATE<br />

--------------- --------------- ---------<br />

GEORGE SAND 12-MAY-46<br />

ROBERT JAMES 23-AUG-37<br />

NANCY LEE 02-FEB-47<br />

VICTORIA LYNN 20-MAY-49<br />

FRANK PILOT 11-NOV-42<br />

To calculate each person’s age, compute the months between today’s date and their birth dates,<br />

and divide by 12 to get the years. <strong>The</strong> division will print the age with a decimal component.<br />

Because most people over the age of 7 don’t report their age using portions of years, apply a<br />

FLOOR function to the computation.<br />

select FirstName, LastName, BirthDate,<br />

FLOOR(<br />

MONTHS_BETWEEN(SysDate,BirthDate)/12<br />

) AS Age<br />

from BIRTHDAY;<br />

FIRSTNAME LASTNAME BIRTHDATE AGE<br />

--------------- --------------- --------- -------<br />

GEORGE SAND 12-MAY-46 61<br />

ROBERT JAMES 23-AUG-37 70<br />

NANCY LEE 02-FEB-47 61<br />

VICTORIA LYNN 20-MAY-49 58<br />

FRANK PILOT 11-NOV-42 65<br />

Combining Date Functions<br />

You are hired on February 28, 2008, at a great new job, with a starting salary that is lower than<br />

you had hoped, but with a promise of a review the first of the month after six months have<br />

passed. If the current date is February 28, 2008, when is your review date?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!