Converting dates from different centuries - oracle

I have staging table which contains date as string with format 'mm/dd/yy'. I have Oracle 11g procedure to convert the string to date format before loading into main table. I'm using to_date('03/20/34','mm/dd/rr') to convert into date format which is giving wrong output as 03/20/2034 whereas the correct date is 03/20/1934. Please help me out to get the correct output where my table contains dates from both centuries.

"I'm using to_date('03/20/34','mm/dd/rr') to convert into date format which is giving wrong output as 03/20/2034 whereas the correct date is 03/20/1934. "
RR was a hack Oracle introduced in the last Millennium as part of the fight to resolve the Y2K bug. The standard date mask YY defaults the century to the current century. But in 1999 it was more likely that 01/01/00 meant 01/01/2000 rather than 01/01/1900. So the RR hack was to derive the century for dates using fixed windows pivoting on 00: values 00-49 are given century 20, 50-99 are given 19. Clearly some of the time this guess would be wrong, but the data corruption introduced was of a lower level than defaulting all dates to century 19.
The key point is, the windows are fixed. It was intended to be a temporary solution, because there wasn't time to switch all the legacy systems to use four-digit years before 2000 arrived. But the vision was always that all systems would be fixed in the long term, even if only through retirement or replacement. Certainly nobody expected that new systems would be built supporting two-digit years.
It is now 2017 and there is no excuse for systems to still be using two-digit years. Back in the old days storage was expensive, and shaving two digits from a date was a valuable space saving. Now it is just sloppiness.
Which obviously doesn't help you solve your problem. The short answer is there is no way to change the pivot used by RR. The best solution would be to enforce stricter validation on the data input aspect of your system, and insist on four-digit years. Whether that's feasible depends on your office politics. The other solution is to write your own conversion function:
create or replace function my_to_date (p_str varchar2) return date as
begin
if to_number(substr(p_str, 7) <= 35 then
return to_date(substr(p_str, 1, 6)||'19'||substr(p_str, 7), 'dd/mm/yyyy');
else
return to_date(substr(p_str, 1, 6)||'20'||substr(p_str, 7), 'dd/mm/yyyy');
end;
Obviously you'll need to define the actual rules for deciding whether to use 19 or 20.

I also encountered an issue like this, when inserting date values from the late 90s. The format in the script I was given read DD-MON-YY, so the database read that as 20YY, instead of 19YY.
My very inelegant solution was to open the raw data file and simply add a "19" before the YY year values.

Related

Oracle SQL Output to Excel - Date format issue

I ran an SQL Query for Oracle which consists of Invoice date and Check date. When these data are copied on to an Excel Spreadsheet as text, it's dispayed as ex: "13-10-31" (Oct 31, 2013). However, when converted to date format, it's displayed as "10/13/1931". I've tried different date types but it always recognizes as the first part of the text as the day, then month, then year. I need these values to be setup as a date format as I need to calculate Days Payable Outstanding and other related ratios.
Is there any way to convert these values so that Excel recognizes the day, month, and year correctly? Would there be a macro that could automate this process for existing data and data that will be added in the future?
Thank you in advance.
Firstly, I hope the data type of your date column is DATE.
Secondly, the date should always have year as YYYY and not just YY. The world has already learned from Y2K bug.
If above two points are met, then while displaying use to_char(date_column, 'mm/dd/yyyy'). Thus, with YYYY format, there won't be any confusion between year and other fields.

What is a reasonable year datatype in Oracle?

Two possibilities come into my mind:
NUMBER(4)
DATE
Pro NUMBER(4):
No duplicate entries possible if specified as UNIQUE
Easy arithmetic (add one, subtract one)
Con NUMBER(4):
No Validation (e.g. negative numbers)
Pro DATE:
Validation
Con DATE:
Duplicate entries are possible ('2013-06-24', '2013-06-23', ...)
Not so easy arithmetic (add one = ADD_MONTHS(12))
As additional requirement the column gets compared with the current year EXTRACT (YEAR FROM SYSDATE). In my opinion NUMBER(4) ist the better choice. What do you think, is there another option I have missed?
You can restrict a date column to only have one entry per year if you want to, with a function-based index:
create unique index uq_yr on <table> (trunc(<column>, 'YYYY'));
Trying to insert two dates in the same year would give you an ORA-00001 error. Of course, if you don't want the rest of the date then it may be unhelpful or confusing to hold it, but on the other hand there may be secondary info you want to keep (e.g. if you're recording that an annual audit happened, holding the full date might not hurt anything). You could also have a virtual column (from 11g) that holds the trunc value for easier manipulation, perhaps.
You could also use an interval year(4) to month data type, and insert using numtoyminterval(2013, 'year'), etc. You could do interval arithmetic to add and subtract years, and extract to get the year back out as a number. That would probably be more painful than using a date though, overall.
If you're really only interested in the year (and you are not holding the month in a different column!) then a number is probably going to be simplest, with a check constraint to make sure it's a sensible number - number(4) doesn't stop you inserting 2.013 when you meant 2,013 (though you need to be converting from a string to hit that, and not have an NLS parameter mismatch), which would be truncated to just 2.
You've quite well summed up the pros/cons.
Provided that you name clearly your field so that it's easy to understand that it contains a year information, I would go with a NUMBER(4) for simplicity & storing no more or less than what is necessary. And even if there is no validation, IMO negative years are valid :)
Depending on your use case you might also consider building a one-off date (dimension) table and linking to a specific row via ID. That way, you have access to more information which you could later add to the dinemsion table (leap year etc.) and the entries in your dimension can be validated on creation.

Oracle 10g Database Date Conversion to 'yyyy-iw' problems?

I'm not sure if this is a question or more of an exploration of a possible bug or a question about a better way to do handle this.
I have a rollup report that uses the
select column1id, column2date
from table1
where to_char(column2date,'yyyy-iw') = to_char(to_date('2012-12-31','yyyy-mm-dd'),'yyyy-iw')
The line: to_char(to_date('2012-12-31','yyyy-mm-dd'),'yyyy-iw') is converting to 2012-01, wrapping back to the beginning of the year.
Digging a bit further I find that the date 2012-12-31 is neither included in week: 2012-52 nor is it included in 2013-01, and 2012-53 doesn't return any data either... so I'm at a loss of what's going on here.
https://forums.oracle.com/forums/thread.jspa?threadID=995899
Ravi Kumar wrote: you need to use IYYY in format.
BluShadow wrote: ... When it calculates the YYYY and IW these are independant of each other so it won't reduce the YYYY output to [2013] just because you have included IW in the format mask. It looks at components of the mask and not the whole thing in combination.
select to_char(to_date('2012-12-31','yyyy-mm-dd'),'iyyy-iw') from dual;
returns 2013-01.
I think your WHERE clause should be:
where to_char(column2date,'iyyy-iw') = to_char(to_date('2012-12-31','yyyy-mm-dd'),'iyyy-iw')

Oracle to SQL Server: Date Conversion & Format

I'm in the process of migrating our Data Warehouse from Oracle to SQL Server 2012.
One piece of code I use a hundred times a day in Oracle is changing the date format for a query by using something like:
To_Char(entry_date,'WMMYY') = 30612
The above allows me to grab the date (default format = DD-MON-YY eg 01-JAN-12) and change the format for the week specified (the third week of June in the above example) by simply listing the week desired.
In my mind the above is very simple and easy to use. I can change it to whatever format I want (MMYY, MMYYYY) etc. without any issues. So far I cannot figure out an easy way to do this in SQL Server 2012 and it's really starting to bother me. It's datetime2 in SQL Server.
I'm finding stuff for CAST(), CONVERT(), DATEPART() but from what I've seen there is all kinds of wacky coding and number codes (like 101, 102, I don't understand why this is) required which just seems extraneous and over complicated to me.
Have I just not found what I'm looking for yet or is this just the way it is with SQL Server? I just want to be able to do something simple like grab all the data that was entered in during the month of june or the second week of october without having to add 200 extra characters of code.
It's a switch. You are using SQL Server 2012, so look into FORMAT() function. http://technet.microsoft.com/en-us/library/hh213505%28SQL.110%29.aspx I'm not, so unfortunately I can't test anything out for you - but there are some date formatting improvements.
For those of us who aren't - , there are things you can do that will be great - once you get used to them. This isn't specific to SQL Server - but Week of Month varies from organization to organization - so you'd have to define that to get a good example for 3rd week of month, etc. Also, when using DATEPART with week or day arguments, what gets returned is determined by the setting for the first day of the week (1 -7 - ##DATEFIRST). The numeric styles for CONVERT (again check out FORMAT() if you're on 2012) are definitely not as intuitive - but you'll probably be using the same styles over and over, and you'll have them memorized quickly. If you are set on 'WMMYY' or just use something over and over that doesn't have a satisfactory built in solution- create a UDF.
data that was entered in during the month of june
DATEPART(mm,#date) = 6
current month
month(getDate())
Or this week
DATEPART(ww,GetDate())

Significance of date 02/31/2157?

I work in a large scale IT support environment. Twice now we have seen an invalid date of 02/31/2157 being inserted in an Oracle DATE column. So far I have not been able to reproduce this problem, but it appears to be happening occasionally when a user attempts to save '00/00/0000' into the column. I believe the value is originating from a PowerBuilder DataWindow update.
The application uses myriad libraries for all sorts of technologies, so this question may be a bit vague, but...
Has anyone seen the date 02/31/2157 in some established library that Oracle could be defaulting to when some other invalid date is entered? Perhaps an end-of-time concept analogous to the beginning-of-time date of 1/1/1970?
From http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#i1847"
Oracle uses its own internal format to
store dates. Date data is stored in
fixed-length fields of seven bytes
each, corresponding to century, year,
month, day, hour, minute, and second.
2157-256 = 1901, which seems suspiciously close to a possible epoch of 1/1/1900 (or 12/13/1901 - which is the rollover date for the Year 2038 Problem)
I'd guess that it is storing either 0x00 or 0xFF in the date bytes, then getting confused when it decodes it. (How does it deal with month 255?)
Turns out this was a powerbuilder issue. The field was created in the datawindow as required, but was programmatically changed to be non-required before saving. So a null value was being saved to a non-null database column, and powerbuilder inserted some dummy date instead of just throwing an error.
I remember getting a weird value when saving an invalid date. IIRC it was in PB 9 and we had to get an EBF for it. It was a problem with Date Editmasks and entering an invalid date that wasn't rejected. Sorry I don't have more details.

Resources