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
Describe the bug
When reading
datetimeoffsetcolumns with scale 0, 1, or 2 from SQL Server usingSqlDataReader.GetFieldValue<DateTimeOffset>(), the returnedDateTimeOffsetvalue 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 aDateTimeOffsetwith 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:30is returned as2024-01-15 10:30:00.00 +00:00(or other incorrect offset depending on data length).Steps to reproduce
Environment