Dan
Dan

Reputation: 67

MS Access 2010 SQL query is rounding automatically to whole numbers

All,

I'm running the SQL query below in MS Access 2010. Everything works fine except that the "a.trans_amt" column is rounded to a whole number (i.e. the query returns 12.00 instead of 12.15 or 96.00 instead of 96.30). Any ideas? I'd like it to display 2 decimal points. I tried using the ROUND function but didn't have any success.

Thanks!

INSERT INTO [2-Matched Activity] ( dbs_eff_date, batch_id_r1, jrnl_name, 
ledger, entity_id_s1, account_s2, intercompany_s6, trans_amt, 
dbs_description, icb_name, fdt_key, combo )
SELECT a.dbs_eff_date, 
a.batch_id_r1, 
a.jrnl_name, 
a.ledger, 
a.entity_id_s1, 
a.account_s2, 
a.intercompany_s6, 
a.trans_amt, 
a.dbs_description, 
a.icb_name, 
a.fdt_key, 
a.combo
FROM [1-ICB Daily Activity] AS a 
INNER JOIN 
(
SELECT 
b.dbs_eff_date, 
b.batch_id_r1, 
b.jrnl_name, 
sum(b.trans_amt) AS ["trans_amt"], 
b.icb_name 
FROM [1-ICB Daily Activity] AS b 
GROUP BY dbs_eff_date, batch_id_r1, jrnl_name, icb_name 
HAVING sum(trans_amt) = 0
)  AS b 
ON (a.dbs_eff_date = b.dbs_eff_date) AND (a.batch_id_r1 = b.batch_id_r1) AND 
(a.jrnl_name = b.jrnl_name) AND (a.icb_name = b.icb_name);

Upvotes: 1

Views: 714

Answers (1)

Parfait
Parfait

Reputation: 107687

Essentially, you are attempting to append decimal precise values to an integer column. While MS Access does not raise a type exception it will implicitly reduce precision to fit the destination storage. To avoid these undesired results, set the precision type ahead of time.

According to MSDN docs, the MS Access database engine maintains the following numeric types:

REAL        4 bytes     A single-precision floating-point value with a range of ...
FLOAT       8 bytes     A double-precision floating-point value with a range of ...
SMALLINT    2 bytes     A short integer between – 32,768 and 32,767.
INTEGER     4 bytes     A long integer between – 2,147,483,648 and 2,147,483,647.
DECIMAL    17 bytes     An exact numeric data type that holds values ...

And the MS Access GUI translates these as Field Sizes in table design interface where the default format of Number is Long Integer type.

Byte           — For integers that range from 0 to 255. Storage requirement is a single byte.    
Integer        — For integers that range from -32,768 to +32,767. Storage requirement is two bytes.    
Long Integer   — For integers that range from -2,147,483,648 to +2,147,483,647 ...    
Single         — For numeric floating point values that range from -3.4 x 1038 to ...    
Double         — For numeric floating point values that range from -1.797 x 10308 to ...
Replication ID — For storing a GUID that is required for replication...
Decimal        — For numeric values that range from -9.999... x 1027 to +9.999...

Therefore, in designing your database, schema, and tables, select the appropriate values to accommodate your needed precision. If not using the MS Access GUI program, you can define type in a DDL command:

CREATE TABLE [2-Matched Activity] (
    ...
    trans_amt DOUBLE,
    ...
)

If table already exists consider altering design with another DDL command.

ALTER TABLE [2-Matched Activity] ALTER COLUMN trans_amt DOUBLE

Do note: if you run CREATE and ALTER commands in Query Design window, no prompts or confirmation will occur but changes will render.

Upvotes: 1

Related Questions