Best way to store time (hh:mm) in a database

前端 未结 15 2159
面向向阳花
面向向阳花 2020-11-29 18:58

I want to store times in a database table but only need to store the hours and minutes. I know I could just use DATETIME and ignore the other components of the date, but wha

15条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 19:26

    Instead of minutes-past-midnight we store it as 24 hours clock, as an SMALLINT.

    09:12 = 912 14:15 = 1415

    when converting back to "human readable form" we just insert a colon ":" two characters from the right. Left-pad with zeros if you need to. Saves the mathematics each way, and uses a few fewer bytes (compared to varchar), plus enforces that the value is numeric (rather than alphanumeric)

    Pretty goofy though ... there should have been a TIME datatype in MS SQL for many a year already IMHO ...

提交回复
热议问题