Skip to content

SqlDataReader.GetDecimal silently truncates decimal(38,18) precision #22

@priyankatiwari08

Description

@priyankatiwari08

Describe the bug

When reading a decimal(38,18) column from a SqlDataReader, the precision is silently truncated to decimal(28,14). This causes financial calculations to lose precision and produce incorrect results.

To reproduce

using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();

// Create table with high-precision decimal
await new SqlCommand("CREATE TABLE #Test (Value decimal(38,18))", connection).ExecuteNonQueryAsync();
await new SqlCommand("INSERT INTO #Test VALUES (12345678901234567890.123456789012345678)", connection).ExecuteNonQueryAsync();

using var reader = await new SqlCommand("SELECT Value FROM #Test", connection).ExecuteReaderAsync();
await reader.ReadAsync();

var value = reader.GetDecimal(0);
Console.WriteLine(value); // Expected: 12345678901234567890.123456789012345678
                          // Actual:   12345678901234567890.12345678901235 (truncated!)

Expected behavior

GetDecimal should return the full decimal(38,18) value without precision loss.

Actual behavior

The value is silently truncated to approximately 28 significant digits. No exception or warning is thrown.

Further technical details

Microsoft.Data.SqlClient version: 6.0.0
Target framework: .NET 8.0
SQL Server version: SQL Server 2022 (16.0.4135)
Operating system: Windows 11 23H2

The issue appears to be in the TDS parser where the decimal scale/precision from the column metadata isn't fully propagated to the CLR decimal type conversion.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions