Search code examples
c++db2decimaloledb

DB2 decimal fields come back as negative DBTYPE_NUMERIC from OLEDB


Using OLEDB from C++ I get DBTYPE_NUMERIC from DB2 for decimal fields and convert to DECIMAL like so (error checking omitted):

...
CDynamicAccessor* pAccessor = GetAccessor();
...
DBTYPE dbT=0;
pAccessor->GetColumnType(nIndex, &dbT);
if (dbT == DBTYPE_NUMERIC)
{
   DB_NUMERIC* pNum = (DB_NUMERIC*)pAccessor->GetValue(nIndex);
   DECIMAL dec;
   DECIMAL_SETZERO(dec);
   dec.sign = pNum->sign; // <--this comes back as 1 for positive fields and 0 for negative
   dec.scale = pNum->scale;
   memcpy(&dec.Hi32, pNum->val + 8, 4);
   memcpy(&dec.Lo64, pNum->val, 8);
   ....
}

What I don't understand is why DB_NUMERIC::sign coming back as 1 for positive decimal fields in DB2 and 0 for negative. Is this correct behavior? By definition here it's the opposite for DECIMAL - 0 should be for positive. Is it a db/field setting? In database everything looks positive. And if I set a value in db as negative it comes back positive. So the sign is turned on in the returning DB_NUMERIC structure for positives and turned off for negative. It's a reverse/inverse of what seems to be expected. What's wrong? BTW: I can't find any docs on DB_NUMERIC that is in oledb.h anywhere. Please help. Thanks a lot in advance!


Solution

  • Found an obscure doc in MSDN which confirms that DB_NUMERIC and DECIMAL treat the sign almost opposite ways.