Wild stuff: Nines complement date format

For German readers: Es gibt eine deutsche Version dieses Blogeintrags.

 
Photo from Picture Taker 2 at flickr.com

This is the first in what I hope to be a series of some wild stuff I have to deal with: Strange formats, encodings and data modeling decisions that I or others come across. 

This post is about a date format that we are calling Nine’s complement. I couldn’t find anything about this on wikipedia, and googling I could only find some mention in an obscure IBM documentation.

What we found

Here’s what we found: In one of SAP’s screens you have a ‘valid to’ date field. You can enter some date until the business information was valid (like ‘31.12.2006’). When we looked for the corresponding field in the database, the column was easy to find (called VALIDTO), but it contained values like ‘79938768’ that surely didn’t look like a date. Also the column was defined as a CHAR(8) field. CHAR(8) is not unusual in itself for a date column, storing the date in a format like ‘YYYYMMDD’ so you can sort a date by sorting a number. In that way, ‘31.12.2006’ would be stored as ‘20061231’.

It took some asking around to figure out what was going on with the strange values we were seeing in the database. What you do is you take the YYYYMMDD-format and subtract that from 99999999. Here is the whole story:

Date 31.12.2006
as YYYYMMDD 20061231
   
8 Nines: 99999999
minus Date-String 20061231
Date in Nine’s Complement 79938768

 

In order to convert the values found in the database back into a proper date, you can perform the same operations in reverse (i.e. subtract the value from ‘8 Nines’, and you get a date in the YYYYDDMM-format).

But why?

The “official” reason behind this format is that you reverse the sorting order of the dates, so that the most recent date comes first. This way, you can simulate a descending sort order in a database that does not support this concept.

As we were working with an SAP system and SAP has built an abstraction layer on top of the underlying databases, that may well be the case here. Some of the code dates back to the early 1980s, and databases were quite different back then. Sometimes I think that they even built there own database independent indexing mechanism, although these days most indexes defined in SAP correspond to indexes on the database level.

However, it is still not clear to me why defining a descending sort order in an index is important . I’ve asked a question on StackOverflow about why you would defined a descending order in an index definition, and the answers pointed to reasonably obscure situations that do not apply to the situation we were seeing. Unless there is stuff like clustered tables involved, the ordering of an index shouldn’t be important, if you’re doing an order, a binary search or traversing a b-tree, the order would not have a material effect on performance.

It would be quite interesting to evaluate the performance impacts of a “normal” date storage and a regular ascending index. I’m quite confident that there will not be much of a difference. So I can only assume that if there was a reason in the past to do this, the reasoning no longer applies. However, my guess is that someone optimized something that didn’t need to be optimized. Probably some “old COBOL mainframe programmer” from back in the days when all you could do was sequential access to a file …


Posted

in

by

Tags:

Comments

7 responses to “Wild stuff: Nines complement date format”

  1. […] Putzige Sachen: Datum im Neuner-Komplement March 5, 2010 by ThorstenFiled under: Deutsch  Für englisch-sprachige Leser: There is an English version of this post. […]

  2. Henrik Liliendahl Sørensen Avatar

    It is fantastic what you find in these old ERP solutions that were born many years ago. The skin today looks modern, but the raw data is kept in many old fashioned ways.

    This goes for SAP and also another solution I have worked with being what today is MS Dynamics AX ,. The latter one was born many years ago by two brothers in a garage in Copenhagen.

    The two brothers became very rich when M$ bought the system.

    One of the brothers is a celeb in Denmark often seen with his fancy new wife who looks very modern on the outside but probably… oh, sorry Thorsten, this is a respectable blog.

  3. Phil Simon Avatar

    I have seen the same thing many times. One vendor I know has renamed one of its apps many times but the fundamental tables have essentially stayed the same. It’s much easier for the marketing folks to rebrand something than it is for developers to update every field, table, index, etc…

  4. […] Wild stuff: Nines complement date format – Thorsten Radde provides a great example of the unique data quality challenges presented by legacy applications by explaining the date format known as Nine’s complement.  […]

  5. Jonathan Welstead Avatar
    Jonathan Welstead

    As an old Cobol Programmer we would often store dates in nines compliment as a secondary key to an Indexed file. When one wanted to read the file in descending date order it could be opened and read sequentially using that key as the index.

  6. John Avatar
    John

    I ran into this in 2008… wish you’d posted it back then! Spent a while figuring it out. The fact that this appears in SAP just affirms my suspicion that SAP is an ancient pile of poo. Thank heaven I’ve never had to work with it directly.

  7. ken holmes Avatar
    ken holmes

    The nines complement date was used extensively in PARS (I learned it as a PARS date). IBM has the Programmed Airline Reservation System that at one time, many of the legacy carriers used (except AA’s SABRE). This allowed the sort sequence that allowed faster search for the earliest date. This dates back to the 1960’s.

Leave a Reply

Your email address will not be published. Required fields are marked *