question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cutting off DateTime precision?

See original GitHub issue

Hi, I haven’t been able to verify that this is a SqlHydra issue and not a SqlKata issue, but I’m having some DateTimes getting truncated to the second in my queries.
I’ve created a query expression to log a select query when executing it:

let private stringifyQuery (query:SqlKata.Query) =
    let compiler = SqlKata.Compilers.SqlServerCompiler()
    let sql = compiler.Compile(query)
    let bindings = 
      String.concat "; " 
        [ 
          for KeyValue (k, v) in sql.NamedBindings -> 
            match v with 
            | :? DateTime as d -> sprintf "(%A: %s)" k (d.ToString("yyyy-MM-dd hh:mm:ss.fffffff"))
            | _ -> sprintf "(%A: %A)" k v
        ]
    sprintf "'%s' with parameters '%s'" sql.Sql bindings

type LoggingSelectBuilder<'a, 'b, 'c when 'c :> Common.DbDataReader> (reader : ('c -> unit -> 'a), ct:ContextType) = 
  inherit SelectAsyncBuilder<'a, 'b, 'c>(reader, ct)

  member this.Run(state: QuerySource<'a, SqlKata.Query>) =
    printfn "%s" <| stringifyQuery state.Query
    base.Run(state)
    
let loggingSelect reader connStr = 
  LoggingSelectBuilder(reader, (ContextType.Create (openContext connStr)))

I have a query like:

  let! myCount = 
    loggingSelect MyDB.HydraReader.Read connStr {
      for record in table<MyDB.dbo.MyTable> do
      where (record.entry_timestamp > ts)
      select (minBy record.entry_timestamp)
    } 
    |>> Array.ofSeq
    |>> Array.head

  printfn "results in: %s" (myCount.ToString("yyyy-MM-dd hh:mm:ss.fffffff"))

which outputs:

'SELECT MIN([MyTable].[entry_timestamp]) FROM [MyTable] WHERE ([MyTable].[entry_timestamp] > @p0)' with parameters '("@p0": 2022-07-15 09:40:42.6078010)'

results in: 2022-07-15 09:40:42.6077917

it seems like I have to add a full second to the timestamp in the query to get the results I want.
Do you think this is a SqlKata issue or a SqlHydra issue? Do you have any recommendations for how to debug this? A SqlKata developer claims that SqlKata sends raw DateTime parameters to the DB server unaltered in this issue, and is correct as far as what I can tell from the SqlKata source. But I also can’t see in SqlHydra source anywhere that the truncation would be happening.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanMarrcommented, Jul 22, 2022

No worries, that stuff is hard. I personally never use the devcontainer setup, mostly because I prefer using Visual Studio over vscode. So I just do docker-compose up instead and then use VS.

As for modifying the table, maybe you can append the new table create script to the install.sql file and then re-run the script?

At least that’s what we did for the test tables created in the postgres install.sql file.

1reaction
JordanMarrcommented, Jul 21, 2022

Nice work on the testing! It looks as though this issue can be resolved in SqlHydra. A similar mechanism is already in place for SqlHydra.Npgsql jsonb types that we can use here. I think it should be pretty straight forward change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Is there a better way to trim a DateTime to a specific ...
I like this method. Someone mentioned it was good to preserve the Date Kind, etc. This accomplishes that because you dont have to...
Read more >
DateTime loses precision when bound as a parameter #1511
DateTime ticks are truncated when they are used as a parameter. ... now use SQLCommand instead of Dapper conn.Open(); var command = conn....
Read more >
Is there a cutoff when using datetime and date datatypes in ...
I have a column titled achieved_date. In my query most of the values for that column are 2016-09-30 23:59:59.997. I then have a...
Read more >
How to keep the precision of datetime when taking into ...
You don't actually want a datetime array, you want a duration array. That's easy. First remove the fractional seconds. There's no need to ......
Read more >
Missing milliseconds, when parsing Java 8 date-time, if ...
This seems to be a issue with the deeper dateTimeFormatter. The issue is if a Json string contains a date with sub second...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found