Search code examples
sql-serversqldatatypes

Data type that only stores hour and minute in MSSQL?


Is there any data type in MSSQL that allows me to store only the hour and minutes of a time value?


Solution

  • Not exactly.

    The closest what you can get is time(0):

    Specified scale | Result (precision, scale)
    ---------------------------------------------
    time(0)         | (8,0)
    

    From the documentation:

    Character length: 8 positions minimum (hh:mm:ss) to 16 maximum (hh:mm:ss.nnnnnnn). [...]

    If you realy only need the Hour and minute part, you could also have two tinyint columns.

    If your question is just about the display, then use ToString("HH:mm") on DateTime. If you only store the time, you get a TimeSpan in .net, hence I create a new DateTime instance in this example:

    var dateTime = new DateTime(timeSpan.Ticks);
    var formattedString = dateTime.ToString("HH:mm");