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".
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.
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.
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.
No. It is not possible to reduce the precision of the time
datatype any further than time(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With