I was trying to apply the TRUNC function for a decimal(18,2) column. My aim is to just get the whole number without any decimals and store it as VARCHAR
DECIMAL_COLUMN_NAME[decimal(18,2)] --> TRUNC(DECIMAL_COLUMN_NAME,0) --> DECIMAL_COL_NAME_VCHAR[varchar(18)]
I expect like
1234.98 --> TRUNC(1234.98,0) --> 1234 in a varchar column
But I am getting the result as 1234.98.
Amazingly this function works all fine if I have the TARGET Column Datatype as INTEGER instead of VARCHAR
I would expect the integer datatype to work as an integer cannot store decimal points so it just strips them off.
It looks like the trunc() function simply changes all decimanl positions to zeroes but keeps the values.
If you simply make the make the datatype in the query transform an integer but keep the target column as a varchar, it should work fine with the trunc() function.
DECIMAL_COLUMN_NAME[decimal(18,2)] --> TRUNC(DECIMAL_COLUMN_NAME,0) as int --> DECIMAL_COL_NAME_VCHAR[varchar(18)]
What is the datatype of the DECIMAL_COLUMN_NAME? decimal.
What is the returning datatype of the TRUNC() function? decimal!!!
Now you assign this decimal value to a varchar column, hence the decimal-to-varchar code kicks in.
My statement would be that
trunc(1234.98,0) = 1234.00 (decimal)
decimal-to-varchar conversion is using the format 99999990D.000000 and therefore I would expect the value ‘1234.00’ in the varchar.
To answer the question, what I would do is
ltrim_blanks(to_char(trunc(1234.98, 0), ‘999999999999’))