Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server date/time oddity.

SQL Server date/time oddity.

Scheduled Pinned Locked Moved Database
databasesql-servercsharpsysadminhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Septimus Hedgehog
    wrote on last edited by
    #1

    I run a query in SSMS. A date/time column displays correctly like "2013-12-15 12:19:43.583". I get the data table object back into my app and iterate through the data rows. Converting the column using DateTime always "loses" the millisecond component. I tried converting it to a string just to see if it was coming through that way, but no, the millisecond is always "lost". I then changed the query to cast the datetime column into two columns, one for the date and one for the time. Converting the time column to a DateTime now correctly contains the millisecond component. If the column in the database is defined as datetime why am I not getting the millsecond component? There's no problem converting the value in the row using DateTime but the loss of the millsecond puzzles me because I thought a database datetime column would be wholly compatible with a Dotnet DateTime object? Are my pudgy fingers, and thus by association, my pudgy brain, doing something wrong in my code?

    If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

    M J Richard DeemingR 3 Replies Last reply
    0
    • S Septimus Hedgehog

      I run a query in SSMS. A date/time column displays correctly like "2013-12-15 12:19:43.583". I get the data table object back into my app and iterate through the data rows. Converting the column using DateTime always "loses" the millisecond component. I tried converting it to a string just to see if it was coming through that way, but no, the millisecond is always "lost". I then changed the query to cast the datetime column into two columns, one for the date and one for the time. Converting the time column to a DateTime now correctly contains the millisecond component. If the column in the database is defined as datetime why am I not getting the millsecond component? There's no problem converting the value in the row using DateTime but the loss of the millsecond puzzles me because I thought a database datetime column would be wholly compatible with a Dotnet DateTime object? Are my pudgy fingers, and thus by association, my pudgy brain, doing something wrong in my code?

      If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Thankfully in all my years as a LOB developer I have not once had to utilise the millisecond information so I have no idea :-D I'm assuming this is an intellectual exercise only!

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • S Septimus Hedgehog

        I run a query in SSMS. A date/time column displays correctly like "2013-12-15 12:19:43.583". I get the data table object back into my app and iterate through the data rows. Converting the column using DateTime always "loses" the millisecond component. I tried converting it to a string just to see if it was coming through that way, but no, the millisecond is always "lost". I then changed the query to cast the datetime column into two columns, one for the date and one for the time. Converting the time column to a DateTime now correctly contains the millisecond component. If the column in the database is defined as datetime why am I not getting the millsecond component? There's no problem converting the value in the row using DateTime but the loss of the millsecond puzzles me because I thought a database datetime column would be wholly compatible with a Dotnet DateTime object? Are my pudgy fingers, and thus by association, my pudgy brain, doing something wrong in my code?

        If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        Nothing to see here. :sigh: Are you using a datareader? If so, using DateTime myDate = (DateTime)reader["MyDateTimeColumn"]; will convert the DateTime from the DB into a string and then convert it back to DateTime, and thus losing precision. Instead use reader.GetDateTime(reader.GetOrdinal("MyDateTimeColumn")); <edit> Instead use DateTime myDate = (DateTime)reader.GetSQLDateTime(reader.GetOrdinal("MyDateTimeColumn")); or DateTime myDate = (DateTime)reader.GetSQLValue(reader.GetOrdinal("MyDateTimeColumn")); </edit> Otherwise you need to update the question with a bit of code.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        Richard DeemingR 1 Reply Last reply
        0
        • J Jorgen Andersson

          Nothing to see here. :sigh: Are you using a datareader? If so, using DateTime myDate = (DateTime)reader["MyDateTimeColumn"]; will convert the DateTime from the DB into a string and then convert it back to DateTime, and thus losing precision. Instead use reader.GetDateTime(reader.GetOrdinal("MyDateTimeColumn")); <edit> Instead use DateTime myDate = (DateTime)reader.GetSQLDateTime(reader.GetOrdinal("MyDateTimeColumn")); or DateTime myDate = (DateTime)reader.GetSQLValue(reader.GetOrdinal("MyDateTimeColumn")); </edit> Otherwise you need to update the question with a bit of code.

          Wrong is evil and must be defeated. - Jeff Ello[^]

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Jörgen Andersson wrote:

          (DateTime)reader["MyDateTimeColumn"]; will convert the DateTime from the DB into a string and then convert it back to DateTime

          Er, no it won't. It will take the DateTime value, box it as an Object, and then un-box it to a DateTime. At no point will the value be converted to or from a string.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          J 1 Reply Last reply
          0
          • S Septimus Hedgehog

            I run a query in SSMS. A date/time column displays correctly like "2013-12-15 12:19:43.583". I get the data table object back into my app and iterate through the data rows. Converting the column using DateTime always "loses" the millisecond component. I tried converting it to a string just to see if it was coming through that way, but no, the millisecond is always "lost". I then changed the query to cast the datetime column into two columns, one for the date and one for the time. Converting the time column to a DateTime now correctly contains the millisecond component. If the column in the database is defined as datetime why am I not getting the millsecond component? There's no problem converting the value in the row using DateTime but the loss of the millsecond puzzles me because I thought a database datetime column would be wholly compatible with a Dotnet DateTime object? Are my pudgy fingers, and thus by association, my pudgy brain, doing something wrong in my code?

            If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            How are you inspecting the value of the column? The debugger is probably using the default .ToString method, which will not display the full value. If you use the format string "yyyy-MM-dd HH:mm:ss.fff", you should see the full value.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Jörgen Andersson wrote:

              (DateTime)reader["MyDateTimeColumn"]; will convert the DateTime from the DB into a string and then convert it back to DateTime

              Er, no it won't. It will take the DateTime value, box it as an Object, and then un-box it to a DateTime. At no point will the value be converted to or from a string.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              Correct me if I'm wrong here (it's quite possible) But assuming SQLServer, it will return a SQLDateTime value, as GetValue will only return dotnet types the SQLDateTime will be internally converted to a dotnet type. The Operator for converting from SQLDateTime to DateTime is an Explicit Operator, and as far as I know this internal conversion will not use explicit operators, so it will be an implicit conversion to String instead. The solution is to either use GetSQLValue or GetSQLDateTime or GetDateTime. <edit>Apparently not GetDateTime either.</edit>

              Wrong is evil and must be defeated. - Jeff Ello[^]

              Richard DeemingR 1 Reply Last reply
              0
              • J Jorgen Andersson

                Correct me if I'm wrong here (it's quite possible) But assuming SQLServer, it will return a SQLDateTime value, as GetValue will only return dotnet types the SQLDateTime will be internally converted to a dotnet type. The Operator for converting from SQLDateTime to DateTime is an Explicit Operator, and as far as I know this internal conversion will not use explicit operators, so it will be an implicit conversion to String instead. The solution is to either use GetSQLValue or GetSQLDateTime or GetDateTime. <edit>Apparently not GetDateTime either.</edit>

                Wrong is evil and must be defeated. - Jeff Ello[^]

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                No, the value will not be converted to a string. Tracing through the code with Reflector, the GetDateTime method returns the value of the SqlBuffer.DateTime property. The indexer calls the GetValue method, which returns the value of the SqlBuffer.Value property, which contains a switch block based on the type returned from the database. For the datetime type, it returns the value of the SqlBuffer.DateTime property. GetSqlDateTime returns the value of the SqlBuffer.SqlDateTime property. Both SqlBuffer.SqlDateTime and SqlBuffer.DateTime construct the value from two integers returned by SQL. Neither property converts the value to a string. It might be slightly more efficient to call GetDateTime if you know that the value is a datetime type, but the indexer and GetValue method will return exactly the same value.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                J 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  No, the value will not be converted to a string. Tracing through the code with Reflector, the GetDateTime method returns the value of the SqlBuffer.DateTime property. The indexer calls the GetValue method, which returns the value of the SqlBuffer.Value property, which contains a switch block based on the type returned from the database. For the datetime type, it returns the value of the SqlBuffer.DateTime property. GetSqlDateTime returns the value of the SqlBuffer.SqlDateTime property. Both SqlBuffer.SqlDateTime and SqlBuffer.DateTime construct the value from two integers returned by SQL. Neither property converts the value to a string. It might be slightly more efficient to call GetDateTime if you know that the value is a datetime type, but the indexer and GetValue method will return exactly the same value.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  Had to download Reflector to check this. Also had to update my understanding about SqlDataReader. Tonight I'll eventually deepfry an author of an article, if I can find it back. :sigh:

                  Wrong is evil and must be defeated. - Jeff Ello[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups