Jul 13 2012
Specifying Oracle Date Formats
I had a question from a business user the other day. I was confident that I knew the answer as soon as I saw the first part of her question. 🙂 To be sure, I waited until we got through the entire discussion. Her question started off with:
I am doing date math — using nvl to set a date to jan 01 2099 when it is null…
Right at that point I could have made good money by betting that she was getting a “non-numeric character was found where a numeric…” message. Sure enough, I was correct. The code she was trying to run looked like this:
NVL(TABLE.DT_COL, '01-JAN-2099')
What’s wrong with that? On the surface, nothing. She was looking at a specific date column from a table, and if that date was null, she was replacing it with a specific (constant) date value. Or at least that was her intent. The problem was that the intentions were being interpreted differently depending on where the report was refreshed. It was working in Desktop Intelligence and failing in Web Intelligence. Why was that, and how could it be fixed?
Oracle Implied Conversions
The problem is that if I don’t specifically tell Oracle how I want my string to be converted to a date, then it will make a guess. Oracle will attempt to parse the string and determine the date, and it will look at the NLS_DATE_FORMAT for hints. However that configuration setting can be overridden by a variety of factors. Business Objects also has a variety of places where date formats can be specified and they can get in the way too.
The way I have generally solved this is to remove any guessing from the process by explicitly defining my date format. Rather than provide the code as written above, I would do the following:
NVL(TABLE.DT_COL, TO_DATE('01-JAN-2099','DD-MON-YYYY'))
Now my string is converted to a date using my supplied format mask, and the dreaded “non-numeric found…” message can be avoided.
I made this suggestion to my business user, and after a quick universe update all of her issues were resolved. The bottom line is that I don’t always have to explicitly define everything, but if I have a chance to do so, I generally try to do so. It may take me a little longer up front, but it will me save time in the long run. Even if someone changes the Oracle standard date format on my server the code I provided will continue to work. 😎
Comments Off on Specifying Oracle Date Formats