Finding the first or second Monday in a month

Syam asked a while ago how we could find something like the first Monday or third Saturday in a month. Well Syam, it’s taken me a while to respond but here we go.

For this example we’ll use sysdate as input but any Oracle date will work. You can also substitute any other day of the week for Monday.

The first day of the month is probably a good place to start:

SQL> select sysdate from dual;


SQL> select trunc(sysdate, 'MONTH') FROM DUAL;


Now that we’ve got that we can find the first Monday with the next_day function. Of course we need to remember the next_day function looks for the next named day after the date provided so we subtract 1 day from the date in case the first is a Monday.

SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday') from dual;


Now that we have the first Monday of the month we can add 7 days to find the second Monday or 14 to find the third.

SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+7 FROM dual;


SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+14 FROM dual;


So from here you can change the day you’re looking for or the week number you want it in.

oracle, date functions, sql, database, database development, pl/sql

Oracle Auto Increment Columns – Part 2

Three separate people have commented on my previous article on How to Create Auto Increment Columns in Oracle asking how they can retrieve the value of an auto increment column for use later in their code. Well Daniel, Shaun and Zach, here’s the answer.

After you have referenced sequence.NEXTVAL for a particular sequence (or it is referenced on your behalf by, say, a trigger), you can then reference sequence.CURRVAL to get the value just used for NEXTVAL.

To illustrate this we’ll use the table, sequence, and trigger created in my previous article.

If we insert a row into the table test, the trigger test_trigger automatically calls test_sequence.NEXTVAL.

SQL> insert into test (name) values ('Matt');

1 row created.

We now have test_sequence.CURRVAL available in that session.

SQL> select test_sequence.currval from dual;


In this simple example we can confirm this is the same value just used with this simple query:

SQL> select * from test
where name='Matt';

---------- ------------------------------
8 Matt

Now if we wanted to use this value in another SQL statement, say for an insert on a table which uses this as a foreign key constraint, we can include it on our insert like this:

SQL> insert into tool (owner_id, tool)
values (test_sequence.CURRVAL, 'hammer');

1 row created.

SQL> select * from tool;

---------- ------------------------------
8 hammer

Of course, if we just want to see the value of test_sequence.CURRVAL we can select it from our favorite table dual.

SQL> select test_sequence.currval from dual;


For the table, sequence and trigger used here see my original article on auto increment fields.

Banner/Oracle User Conference

Yesterday I attended a Banner/Oracle user conference at Wellesley College. This annual, one-day event of largely peer presentations was a great opportunity for networking and knowledge exchange. Here are some of the take-homes I got out of this conference in no particular order. These are just my observations and opinions and pertain to the scope of our institution and environment.

Oracle Database

Marc Kelberman, Oracle pre-sales engineer for higher-ed gave a presentation on RAC/Grid control and SQL Developer.

Very few have moved from 9i to 10g. Those who have, or will soon, are going straight to 10gR2.

It is important to go to the latest patchset for 10gR2.

Oracle’s RAC/Grid technology is very cool, but it is unlikely it would offer much to our small (6,000 student) university. Larger universities may benefit from it.

Grid/RAC requires a shared storage architecture.

It would take a great effort between systems, networking and database administrators to implement RAC/Grid.

Around 75% of the institutions I spoke to were running Oracle on Sun Solaris.

None of the institutions I spoke to were running Oracle on Linux.

None of the institutions I spoke to were running clustering/RAC/Grid Control.

Several institutions are running some type of network attached storage (NAS) to store their data files.

One institution (I believe this was the host institution, Wellesley) is running their Oracle home directories on network attached storage. This allows them to maintain only one oracle home per database version saving hours of work per upgrade.

Most institutions are still relying on cold backups as their primary backup method.

Only a couple institutions I talked to have adopted Oracle RMAN for backups.

Oracle SQL Developer

Marc did a good demonstration of SQL Developer, but this product is hard to appreciate until you’ve used it. Thankfully it’s free, so there’s no good reason not to test drive it.

Identity Management

Dan Sterling, Chief Technology Architect for SunGard Higher Education presented on SunGard HE’s plans for identity management. It looks like they will focus on integrating with third party tools via open standards.

Banner Student Information System

One university mentioned that when a user asks for a modification to their student information system they require that the user submit a request for product enhancement with the vendor before a local modification is made. This seems like a good policy.

Final thoughts

The relatively small number of attendees made this conference great for networking. It’s very interesting to interact with universities of different sizes. Some of the universities in attendance had one administrator for their Oracle databases, application servers and application support, while others have a large staff and highly individualized positions.

Though this conference happens near the end of the academic year it was a nice diversion from summer planning. Beyond the networking, the take-homes are more than worth the day out of work.

oracle, banner, sungard he, summit, conference, higher education, database, database administration, database development

Oracle SQL Developer – A New GUI For Database Development

raptor_image.jpgAt long last Oracle has come up with a modern product to replace SQL*Plus.

Oracle SQL Developer (formerly Project Raptor) was released last month with little fanfare; however its release represents a quantum leap in functionality and ease of use over previous Oracle provided SQL development tools.

SQL Developer is Oracle’s new, free graphical tool that enhances productivity and simplifies database development tasks. With SQL Developer,you can:

  • Browse database objects
  • Run SQL statements and SQL scripts
  • Edit and debug PL/SQL statements
  • Run provided reports
  • Create and save custom reports

Now all these features exist in Quest Software’s product Toad for Oracle so why switch? Well, here’s the hook… SQL Developer is free. Yup, free. It also has another strategic advantage over Toad… SQL Developer is cross platform! I am currently running it side-by-side on Mac OS X and Windows XP, and there is also a Linux version available. The Mac and Windows versions are both extraordinarily easy to install, configure, and use. I can only assume the Linux version follows this trend.

So what’s the down side? Well, I’m personally disappointed that they changed the name from “Raptor” to “SQL Developer”, but that aside, you can’t use it on databases earlier than

A small problem for Toad users will be adjusting to the date format in SQL Developer. Toad defaults to showing the time when displaying date data types while SQL Developer will only show the date. The solution to that is to add a TO_CHAR around the date column in the query (for more information on the TO_CHAR function, check out the Displaying Dates section of my article Oracle, SQL, Dates and Timestamps). Toad users may also miss being able to click on the header of a column to change the sort order of the output, but this can also be overcome by adding an ORDER BY clause to the query.

Setup was quick, taking less than a minute to download, unzip and connect (yes, one minute, you’ve probably spent more time reading this than it takes to install). As you’re setting up SQL Developer you’ll notice that it does not require a TNS Names or other descriptor file to find databases. When you set up a connection you are prompted for the hostname and SID of the database. This assures maximum flexibility but does run the risk of confusing end users, since they can nickname the connections anything they want to.

Below are some screenshots of SQL Developer, but if you’re interested, just go out and get it. It’s free after all, what do you have to loose.

SQL WindowPackage Editing

Table BrowsingReports

Click on the thumbnail for a larger image.

oracle, sql, sql developer, database development, database administration, dba

Oracle Timestamp With Time Zone

Last week Warren left a comment on my story Converting Time Zones in Oracle asking how he could output dates with time zone like: “17-Mar-2006 14:30:00 EST”.

Well, after a bit of digging it turns out the answer is not as simple as it sounds. The traditional Oracle DATE and TIMESTAMP datatypes don’t store time zone information. A workaround might be to store time zone information in a separate column in the table, but that seems like it could cause some confusion.

Well, it looks like in version 9i Oracle has added a new datatype to handle exactly this. The TIMESTAMP WITH TIME ZONE datatype allows a time zone to be stored with a date and time either in offset from UTC or by abbreviation.

The TIMESTAMP WITH TIME ZONE datatype can be declared in the table definition anywhere you would have used DATE or TIMESTAMP. To store a date/time/time zone into a row Oracle has also added the function TO_TIMESTAMP_TZ which acts much like the familiar TO_DATE function, however will recognize TZH, TZM, TZR, and TZD for time zone hour, minute, region, and abbreviation respectively.

To retrieve time zone information you can apply the same new abbreviations to the familiar to_char function when selecting a column of type TIMESTAMP WITH TIME ZONE.

Rather than go into any more detail here, check out this article from Oracle Magazine. It covers the topic very well including example code.

sql, oracle, database administration, database, dba, database development, dbms, pl/sql