locked
DateTime from .NET losing more than a millisecond when saved to SQL Server RRS feed

  • Question

  • I have some code that is saving a DateTime (07/29/2011 02:16:25.7245169) to SQL Server 2008 R2 using a strong typed dataset.  Once saved, the date becomes 07/29/2011 02:16:25:7230000.  I get it that SQL Server only supports three places past the decimal.  At that point I would expect it to round up, round down, or truncate.  I am just wondering why it would drop a whole .0015169.  I guess I would expect 02.16.25.724 or 02.16.25.725, pretty much anything but the 02.16.25.723 that I am getting.  Anyone have any ideas?

     

    Friday, July 29, 2011 8:21 PM

Answers

All replies

  • Try to use datetime2 instead of datetime for your database

    http://stackoverflow.com/questions/1334143/sql-server-datetime2-vs-datetime

     

    Regards

    • Marked as answer by David_Stanley Friday, August 5, 2011 1:14 PM
    Friday, July 29, 2011 11:50 PM
  • Hi David;

    As Gaspard05 stated using SQL server data type datetime2 will give you 7 digits of precision which is compatible with .Net data type DateTime.

    datetime2 (Transact-SQL)

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 30, 2011 1:54 PM
  • Hi David,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance. 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 5, 2011 8:32 AM
  • Yes, this solved my issue.  It was a legacy database from SQL Server 2005, but updating the column fixed the issue.  Still doesn't really explain why the DateTIme SQL Data Type was "rounding" so oddly.
    Friday, August 5, 2011 1:16 PM