Should I store DateTimes as Long (Ticks) in the database?

Is it possible to make life easier by saving DateTime values ​​as longinstead? There are always problems when working with null DateTime values, whether it is saving or retrieving - null DateTimes, invalid DateTimes, etc. Always a pain for work.

It would be wise to just work with the data type long, since you can always create a DateTime from ticks?

Edit: I work with SqlServer and MySql. SqlDateTime is .net output DateTime. There are differences between all three platforms of what is a valid DateTime. How do you deal with these differences?

+5
source share
4 answers

I assume this is a personal preference. I always work with datetime types and don't worry about them.

If you store them as longs, although semantically, they are no longer dates. If you ever wanted to make a request to select all the accounts added on Friday (say), you will have to jump over a few hoops to do this.

+7
source

I can’t come up with any outstanding reason personally, the databases support DateTime themselves and keep them for how long, you can end up shooting yourself in the foot. Let's say you need to be able to run the query “Get all the lines between 3 and 6 o’clock” - if you save them as ticks, you will need to go back to DateTime in the database.

, , , ..

DateTimes , TimeZone, DateTime , , UTC. , DateTime , , , TimeZone. - .

+4

, datetime.

DateTime

NULL, , datetime, , long.

... DateTimes

datetimes datetime? datetime , , . , datetime long.

, long , SQL (select * from table) .

+2

. Sql , DateTime. , , , DateTime , .

+2
source

All Articles