Saving DateTime to Sql Server 2005

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

Hi all,

I've noticed that Sql Server seems to truncate the ticks if you save a
..Net 2.0 DateTime to Sql server.

This is messing up my unit tests; any idea how i can mimic the
truncating that Sql server does?

Thanks
Andy
 
Cor,

I know what the problem is, I need a solution to it.

Such as, can i round the ticks value to a certain accuracy to compare
the values properly?

Andy
 
Andy,

You could try truncating the DateTime to 1/100th of a second :

private DateTime TruncateTo100thOfASecond(DateTime dt)
{
int m = (dt.Millisecond / 10) * 10;
DateTime dtNew = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour,
dt.Minute, dt.Second, m);
return dtNew;
}

You'd need to do this for both the DateTimes about to be compared.

HTH,
Stephen
 
Andy,
I know what the problem is, I need a solution to it.
Every programmer can recalculate a 10/3 milisecond to a millisecond, so that
is your point of accuracy. However there are some rounding problems in this.

I saw an article about this on MSDN maybe can you search yourself.

Cor
 
Thanks Stephen,

I may end up doing this before I put characters into the database, just
so i know how the values will change.
 
Yes, I can round to a lower point of accuracy, however the .Net
rounding in Math doesn't seem to round the same way Sql server does...
which is what lead me to post to begin with. Ideally I'd like to mimic
the rounding sql server does, but it seems easier just to lose some
accuracy.
 
Back
Top