Skip to content

Precision on large decimals is lost on select-query (DB contains correct values) #106

Closed
@olsn

Description

@olsn

When doing a simple select-query on a column with a large decimal (e.g. 1234567891011.1213141), everything above 17 digits is cut (most likely the javascript limit here).
The only solution that I see is to return the content as a string instead of a number. - That way the user can decide for themselves if they wish to use some js-library for big numbers.

Here is some code to reproduce:

const config = require("../utils/config-loader").config;  // just db-credentials
const mssql = require("mssql/msnodesqlv8");

async function run() {
	await mssql.connect(config.mssqlConnectionString);
	const sqlQuery = `
		CREATE TABLE [dbo].[TestLargeDecimal] (
				id VARCHAR(12) NOT NULL,
				testfield DECIMAL(21,7) NOT NULL,
				PRIMARY KEY (id)
		);`;
	await mssql.query(sqlQuery);
	
	await mssql.query(`INSERT INTO [dbo].[TestLargeDecimal] (id, testfield) VALUES (1, 1234567891011.1213141);`);
	const result = await mssql.query(`SELECT * FROM [dbo].[TestLargeDecimal]`);
	console.log(result);
	// --> testfield is missing the last 3 digits and only returns "1234567891011.1213"
	// --> the database contains the correct value
	// --> it doesn't actually matter where the decimal-point is, it's probably simply the javascript max-number-length
	
	// optional cleanup
	// await mssql.query(`DROP TABLE [dbo].[TestLargeDecimal];`);
	await mssql.close();
}

run();

If it is in the C-Code, I can probably not help much here, if it's in the Javascript, you can point me to the right direction and I can possibly find a bugfix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions