Skip to content

SqlDataReader.GetFieldValue<DateTimeOffset>() returns wrong offset for datetimeoffset(0) through datetimeoffset(2) #27

@priyankatiwari08

Description

@priyankatiwari08

Describe the bug

When reading datetimeoffset columns with scale 0, 1, or 2 from SQL Server using SqlDataReader.GetFieldValue<DateTimeOffset>(), the returned DateTimeOffset value has an incorrect UTC offset. The offset appears to be parsed from the wrong byte position in the TDS response.

Expected behavior

GetFieldValue<DateTimeOffset>() should return a DateTimeOffset with the correct UTC offset matching the stored value in SQL Server.

Actual behavior

The offset is shifted — e.g., a value stored as 2024-01-15 10:30:00.00 +05:30 is returned as 2024-01-15 10:30:00.00 +00:00 (or other incorrect offset depending on data length).

Steps to reproduce

using var connection = new SqlConnection(connectionString);
connection.Open();
using var cmd = connection.CreateCommand();
cmd.CommandText = @"
    DECLARE @dt datetimeoffset(1) = '2024-01-15 10:30:00.0 +05:30';
    SELECT @dt AS Value;";
using var reader = cmd.ExecuteReader();
reader.Read();
var result = reader.GetFieldValue<DateTimeOffset>(0);
Console.WriteLine(result);          // Expected: 2024-01-15 10:30:00.0 +05:30
Console.WriteLine(result.Offset);   // Expected: 05:30:00, Actual: 00:00:00

Environment

  • Microsoft.Data.SqlClient version: 6.0.1
  • .NET version: .NET 8.0
  • SQL Server version: SQL Server 2022
  • OS: Windows 11

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions