我已经声明了一个值为23:59:59的时间变量。所以,我需要将它转到24:00。你有什么想法吗?如何四舍五入一个SQL时间变量?
declare @t1 time = '23:59:59'
这只在select语句中是必需的。我知道时间不能作为24:00插入。
http://msdn.microsoft.com/en-us/library/bb677243(v=sql.105).aspx
我已经声明了一个值为23:59:59的时间变量。所以,我需要将它转到24:00。你有什么想法吗?如何四舍五入一个SQL时间变量?
declare @t1 time = '23:59:59'
这只在select语句中是必需的。我知道时间不能作为24:00插入。
http://msdn.microsoft.com/en-us/library/bb677243(v=sql.105).aspx
如何:
--setup
declare @t1 time = '23:59:59'
--declare @t1 time = '00:59:59'
--declare @t1 time = '00:59:29'
--declare @t1 time = '00:00:00'
--solution for rounding to nearest hour
declare @temp1 int = datepart(minute, @t1)/30
set @temp1 = (DATEPART(hour, @t1) % 24) + @temp1
select @t1, cast(@temp1 as nvarchar(2)) + ':00' --not too worried about rpading hours since 1:00 makes sense
--solution for rounding to nearest minute
declare @ss int = datepart(second, @t1)/30
, @mm int = datepart(minute, @t1)
, @hh int = datepart(hour, @t1)
, @oo nchar(2) = N'00' --used for padding
set @hh = (@hh + (@mm/30) * @ss) % 24
set @mm = (@mm + @ss) % 60
select @t1
, substring(@oo + cast(@hh as nvarchar(2)), case when @hh > 9 then 3 when @hh = 0 then 1 else 2 end, 2) --hours rpaded
+ N':'
+ substring(@oo + cast(@mm as nvarchar(2)), case when @mm > 9 then 3 when @mm = 0 then 1 else 2 end, 2) --minutes rpaded
或选择'24:00' –
@TonyHopkinson:给出这个问题的确如此,但我怀疑不是这个意图。 – JohnLBevan
这不会是一个演员,因为24是另一种价值比23:59:59。 – driis
很难说你应该做什么,但不是这样。 24 * 60 * 60,这将是我的第一个想法,然后与DateDiff进行时间比较将是我的第一个想法。 –
24:00不是时候。时间数据类型将午夜存储为0:00:00。你究竟想要做什么? –