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

后端 未结 4 1939
刺人心
刺人心 2021-01-12 11:00

Could life be made easier by saving DateTime values as a long instead? There always seem to be problems when working with null DateTime values, whether storing

4条回答
  •  不知归路
    2021-01-12 11:29

    No, use a datetime column.

    There always seem to be problems when working with null DateTime values

    If your column is nullable, and you for some reason have difficulties when the type of the column is datetime you will also have problems when the type of the column is long.

    ... invalid DateTimes

    How do you get invalid datetimes into a datetime column? One of purposes of using the datetime column to begin with is that sort of validation happens before the data is allowed to be inserted. Much more likely you'll get invalid datetimes when using a naked long.

    Finally, using a long means that viewing your database via simple SQL (select * from table) will produce illegible results.

提交回复
热议问题