What is the correct SQL type to store a .Net Timespan with values > 24:00:00?

GraemeMiller picture GraemeMiller · Dec 14, 2011 · Viewed 109.6k times · Source

I am trying to store a .Net TimeSpan in SQL server 2008 R2.

EF Code First seems to be suggesting it should be stored as a Time(7) in SQL.

However TimeSpan in .Net can handle longer periods than 24 hours.

What is the best way to handle storing .Net TimeSpan in SQL server?

Answer

Tom Chantler picture Tom Chantler · Dec 14, 2011

I'd store it in the database as a BIGINT and I'd store the number of ticks (eg. TimeSpan.Ticks property).

That way, if I wanted to get a TimeSpan object when I retrieve it, I could just do TimeSpan.FromTicks(value) which would be easy.