Working with packed dates in SSIS
Posted
by Jim Giercyk
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Jim Giercyk
Published on Tue, 03 Apr 2012 11:17:35 GMT
Indexed on
2012/04/03
23:30 UTC
Read the original article
Hit count: 355
One of the challenges recently thrown my way was to read an EBCDIC flat file, decode packed dates, and insert the dates into a SQL table. For those unfamiliar with packed data, it is a way to store data at the nibble level (half a byte), and was often used by mainframe programmers to conserve storage space. In the case of my input file, the dates were 2 bytes long and represented the number of days that have past since 01/01/1950. My first thought was, in the words of Scooby, Hmmmmph? But, I love a good challenge, so I dove in.
Reading in the flat file was rather simple. The only difference between reading an EBCDIC and an ASCII file is the Code Page option in the connection manager. In my case, I needed to use Code Page 1140 for EBCDIC (I could have also used Code Page 37).
Once the code page is set correctly, SSIS can understand what it is reading and it will convert the output to the default code page, 1252. However, packed data is either unreadable or produces non-alphabetic characters, as we can see in the preview window.
Column 1 is actually the packed date, columns 0 and 2 are the values in the rest of the file. We are only interested in Column 1, which is a 2 byte field representing a packed date. We know that 2 bytes of packed data can be stored in 1 byte of character data, so we are working with 4 packed digits in 2 character bytes. If you are confused, stay tuned….this will make sense in a minute.
Right-click on your Flat File Source shape and select “Show Advanced Editor”. Here is where the magic begins. By changing the properties of the output columns, we can access the packed digits from each byte. By default, the Output Column data type is DT_STR. Since we want to look at the bytes individually and not the entire string, change the data type to DT_BYTES. Next, and most important, set UseBinaryFormat to TRUE. This will write the HEX VALUES of the output string instead of writing the character values. Now we are getting somewhere!
Next, you will need to use a Data Conversion shape in your Data Flow to transform the 2 position byte stream to a 4 position Unicode string containing the packed data. You need the string to be 4 bytes long because it will contain the 4 packed digits. Here is what that should look like in the Data Conversion shape:
Direct the output of your data flow to a test table or file to see the results. In my case, I created a test table. The results looked like this:
Hold on a second! That doesn't look like a date at all. No, of course not. It is a hex number which represents the days which have passed between 01/01/1950 and the date. We have to convert the Hex value to a decimal value, and use the DATEADD function to get a date value. Luckily, I have created a function to convert Hex to Decimal:
DECLARE @decTab TABLE DECLARE @pos1 VARCHAR(1) = SUBSTRING(@hexValue,1,1) INSERT @decTab SET @decValue = (CONVERT(INT,(SELECT decPos4 FROM @decTab))) + RETURN @decValue END |
Making use of the function, I found the decimal conversion, added that number of days to 01/01/1950 and FINALLY arrived at my “unpacked relative date”. Here is the query I used to retrieve the formatted date, and the result set which was returned:
SELECT [packedDate] AS 'Hex Value', |
This technique can be used any time you need to retrieve the hex value of a character string in SSIS. The date example may be a bit difficult to understand at first, but with SSIS becoming the preferred tool for enterprise level integration for many companies, there is no doubt that developers will encounter these types of requirements with regularity in the future.
Please feel free to contact me if you have any questions.
© Geeks with Blogs or respective owner