Nguyễn Ngọc Duy
Nguyễn Ngọc Duy

Reputation: 1261

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?

Upvotes: 2

Views: 3147

Answers (1)

Christian Gollhardt
Christian Gollhardt

Reputation: 17014

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");

Upvotes: 5

Related Questions