Skip to Main Content
Oracle PL/SQL Programming: A Developer's Workbook
book

Oracle PL/SQL Programming: A Developer's Workbook

by Steven Feuerstein, Andrew Odewahn
May 2000
Intermediate to advanced content levelIntermediate to advanced
594 pages
11h 32m
English
O'Reilly Media, Inc.
Content preview from Oracle PL/SQL Programming: A Developer's Workbook

Intermediate

Q:

19-10.

I would use a combination of TRUNC and NEXT_DAY, as shown here:

DECLARE
   first_monday DATE;
BEGIN
   first_monday := NEXT_DAY (
      TRUNC (SYSDATE, 'MONTH'), 'MONDAY');
   DBMS_OUTPUT.PUT_LINE (first_monday);
END;
/

Q:

19-11.

We’re talking time-zone differences, so the NEW_TIME function should work perfectly well. London is on Greenwich Mean Time, while the folks in Chicago are on Central Standard Time. Here is a function that does the conversion:


/* Filename on web page: chitime.sf */
CREATE OR REPLACE FUNCTION chitime RETURN DATE
IS
BEGIN
   RETURN NEW_TIME (SYSDATE, 'GMT', 'CST');
END;
/

Q:

19-12.

TRUNC to the rescue once again:

CREATE OR REPLACE FUNCTION chitime RETURN DATE
IS
BEGIN
   RETURN TRUNC (SYSDATE, 'MONTH');
END;
/

Q:

19-13.

Well, first you have to truncate the current date/time back to the beginning of the century (which also sets the time component to midnight), then move forward 25 years, make sure you’re on the first day of that year, and move forward nine hours. Here goes:

DECLARE
   amaze_me DATE;
BEGIN
   amaze_me := TRUNC (
        ADD_MONTHS (TRUNC (SYSDATE, 'CC'), 25 * 12), 'MONTH') + 9 / 24;
   DBMS_OUTPUT.put_line (
      TO_CHAR (
         amaze_me,
         'MM/DD/YY HH24:MI:SS'
      )
   );
END;
/

Isn’t this fun?

Q:

19-14.

The four pieces of information displayed are:

30-JAN-99 -> 28-FEB-99
27-FEB-99 -> 27-MAR-99
31-JAN-99 -> 28-FEB-99
28-FEB-99 -> 31-MAR-99

Q:

19-15.

Statement (b) describes accurately the behavior ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Oracle Database 12c PL/SQL Programming

Oracle Database 12c PL/SQL Programming

Michael McLaughlin
Oracle PL/SQL for DBAs

Oracle PL/SQL for DBAs

Arup Nanda, Steven Feuerstein
Oracle PL/SQL For Dummies

Oracle PL/SQL For Dummies

Michael Rosenblum, Paul Dorsey

Publisher Resources

ISBN: 9781449324070Supplemental ContentErrata Page