Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the precision of SQL Server 2008 TIME be reduced to only hours and minutes?

With SQL Server 2008 the time data type has an optional precision argument (default is 7). With this you can control how many fractional decimal places are stored and displayed.

DECLARE @time time(3)
SET @time = GETDATE()
PRINT @time

The above would print this,

10:47:25.347

The documentation says the smallest precision is time(0). This would store and print 10:47:25.

Is it possible to reduce the precision even more, to eliminate/zero out seconds: 10:47?

I know this can be done manually by adding a constraint (DATEPART(seconds, @time) = 0), performing math on data entry to zero out the seconds, and manually format when printing, but I am looking for a simplier way to just define a field in a table as "hours and minutes", in much the same way that the date type allows you to define a field as "just the date, no time component".

like image 540
David Avatar asked Nov 22 '10 15:11

David


People also ask

How do you set precision in SQL?

To store numbers that have fixed precision and scale, you use the DECIMAL data type. In this syntax: p is the precision which is the maximum total number of decimal digits that will be stored, both to the left and to the right of the decimal point.

What is length and precision in SQL?

In SQL Server, the default maximum precision of numeric and decimal data types is 38. In earlier versions of SQL Server, the default maximum is 28. Length for a numeric data type is the number of bytes that are used to store the number. For varchar and char, the length of a character string is the number of bytes.

How do you offset time in SQL?

The time zone offset can be represented as [+|-] hh:mm: hh is two digits that range from 00 to 14 and represent the number of hours in the time zone offset. mm is two digits, ranging from 00 to 59, that represent the number of additional minutes in the time zone offset.


1 Answers

No. It is not possible to reduce the precision of the time datatype any further than time(0)

like image 69
Mitch Wheat Avatar answered Sep 28 '22 06:09

Mitch Wheat