Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Instant loses precision #7

Open
michaeltnguyen opened this issue Aug 6, 2020 · 2 comments
Open

Instant loses precision #7

michaeltnguyen opened this issue Aug 6, 2020 · 2 comments

Comments

@michaeltnguyen
Copy link

michaeltnguyen commented Aug 6, 2020

The easiest way to reproduce this is as follows:

var x = Instant.FromUtc(2020, 7, 2, 0, 0).PlusTicks(1333333);
var y = x.ToDateTimeUtc();
var z = x.ToDateTimeOffset();

var aaa = conn.QuerySingle<Instant>("SELECT @x", new { x }); //2020-07-02 00:00:00.133
var bbb = conn.QuerySingle<Instant>("SELECT @y", new { y }); //2020-07-02 00:00:00.133
var zzz = conn.QuerySingle<Instant>("SELECT @z", new { z }); //2020-07-02 00:00:00.1333333

So precision is being lost somewhere in the round trip.

var xxx = conn.QuerySingle<Instant>(@"
    DECLARE @x datetime2 = '2020-07-02 00:00:00.1333333' SELECT @x"); //2020-07-02 00:00:00.1333333

So precision is being lost when it's sent over to the database, but works fine when reading.

@michaeltnguyen michaeltnguyen changed the title Using a previously retrieved Instant in a WHERE clause gives wrong result Instant loses precision Aug 6, 2020
@michaeltnguyen
Copy link
Author

The issue here is that InstantHandler needs to call

parameter.DbType = DbType.DateTime2

It looks like Dapper + Microsoft.Data.SqlClient causes this issue.

@TheAtomicOption
Copy link

This might be due to the SqlParameter Precision and Scale properties.

Warning
Data may be truncated if the Scale property is not explicitly specified and the data on the server does not fit in scale 0 (the default).
For the DateTime2 type, scale 0 (the default) will be passed as datetime2(7). There is currently no way to send a parameter as datetime2(0). Scales 1-7 work as expected. This problem applies to DateTimeOffset and Time as well.

...Precision and Scale are required for output parameters...

https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparameter.scale?view=dotnet-plat-ext-5.0#System_Data_SqlClient_SqlParameter_Scale

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants