SQL How to get COMP-3 Packed Decimal?

5.8k views Asked by At

I am creating an ASCII output file contain some information and two of my fields require the date and time to be in packed decimal fields (COMP-3). I am using SQL and SSIS to generate my file.

Field Name     Format       Length    Picture
Date           YYYYMMDD     5         S9(8) C-3
Time           HHMMSS       4         S9(6) C-3

I have searched multiple sites and I still can't figure out how to convert a date/time into a packed decimal field. Any help would be greatly appreciated.

The following site http://www.simotime.com/datapk01.htm#TablePackedDecimal was provided for information about Packed fields. I understand how packed fields are used, but don't really understand how to pack a field. Thanks!

1

There are 1 answers

5
William Salzman On BEST ANSWER

If you store an integer date (ie 20120123) as a character string the hex representation would be 0x3230313230313233 where 32 = 2, 30 = 0 etc, which is 8 bytes (ie 32 30 31 32 30 31 32 33) of storage.

In a packed decimal format the representation of the same string would be: 0x020120123F The F is a bit to show that this is an unsigned integer. The other numbers are stored as half of a byte for each digit. So you can see that a common date string would fit into a 5 byte (ie 02 01 20 12 3F ) field.

So, to use this in SSIS you would probably have to do as @billinkc stated above and use a Script Transformation to transform the field. The mechanics of this would be to count the digits in your number, pad with 0's on the left to get your characters up to 9 for comp-3 5 and 7 for comp-3 4 then construct a hexidecimal string with the digits from your date or time and add a F at the end (or a C if your destination expects a signed number).