如何在T-SQL中将time(7)
类型转换为bigint
?
我正在尝试更新包含time(7)
数据的列中的数据,并将其转换为另一种bigint
类型的列。
发布于 2013-03-21 22:36:00
假设当你说“滴答”时,你指的是秒。如果您是指毫秒,请将"ss“更改为"ms”。
Declare @time7 Time(7)
Set @time7 = Convert(Time(7),Getdate())
Select Convert(Bigint,Datediff(ss,0,@time7))
发布于 2013-03-21 22:53:12
datediff
返回一个整数,所以如果你想要一个时间(7)的完整精度,你需要做一些计算。在datediff
中使用microsecond
或nanosecond
可能会导致溢出。
declare @T time(7) = '23:59:58.9999999'
select datediff(second, '00:00', @T) * cast(10000000 as bigint) + right(@T, 7)
结果:
863989999999
在datediff
中使用00:00
而不是0
非常重要。使用0
时,您的时间值将隐式转换为datetime
,上面使用的值将转换为值23:59:59
,因为datetime
将四舍五入为.000、.003或.007。
如果使用datediff
supported bigint
,所有这些都将变得容易得多。
https://stackoverflow.com/questions/15550185
复制相似问题