如何在 SQL 服务器中获取完整的毫秒数?
How can I get the full milliseconds in SQL server?
DECLARE @Now DATETIME2(7) = SYSDATETIME();
SELECT
[Datetime2(1)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(1))),
[Datetime2(2)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(2))),
[Datetime2(3)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(3))),
[Datetime2(4)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(4))),
[Datetime2(5)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(5))),
[Datetime2(6)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(6))),
[Datetime2(7)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(7)))
执行 returns 以下示例值:
300 260 265 264 264 264 264
如何获得更高的精度?
没关系,函数 returns 是一个 int,这不是我所期望的。我应该使用 NANOSECOND,并将其划分。
DECLARE @Now DATETIME2(7) = SYSDATETIME();
SELECT
[Datetime2(1)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(1)))/1000000.0,
[Datetime2(2)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(2)))/1000000.0,
[Datetime2(3)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(3)))/1000000.0,
[Datetime2(4)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(4)))/1000000.0,
[Datetime2(5)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(5)))/1000000.0,
[Datetime2(6)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(6)))/1000000.0,
[Datetime2(7)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(7)))/1000000.0
DECLARE @Now DATETIME2(7) = SYSDATETIME();
SELECT
[Datetime2(1)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(1))),
[Datetime2(2)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(2))),
[Datetime2(3)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(3))),
[Datetime2(4)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(4))),
[Datetime2(5)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(5))),
[Datetime2(6)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(6))),
[Datetime2(7)] = DATEPART(MILLISECOND,CAST(@Now AS DATETIME2(7)))
执行 returns 以下示例值:
300 260 265 264 264 264 264
如何获得更高的精度?
没关系,函数 returns 是一个 int,这不是我所期望的。我应该使用 NANOSECOND,并将其划分。
DECLARE @Now DATETIME2(7) = SYSDATETIME();
SELECT
[Datetime2(1)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(1)))/1000000.0,
[Datetime2(2)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(2)))/1000000.0,
[Datetime2(3)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(3)))/1000000.0,
[Datetime2(4)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(4)))/1000000.0,
[Datetime2(5)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(5)))/1000000.0,
[Datetime2(6)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(6)))/1000000.0,
[Datetime2(7)] = DATEPART(NANOSECOND,CAST(@Now AS DATETIME2(7)))/1000000.0