我已经用完成了 DATETIME2(3)
。
如您在下面的查询中看到的,它更多economic
:
declare @dt1 datetime2(3)
declare @dt2 datetime2
SELECT @DT1 = SYSDATETIME()
SELECT @DT2= SYSDATETIME()
SELECT [THE LENGTH OF DATETIME2]=DATALENGTH(@DT2)
,[THE LENGTH OF DATETIME2(3)]=DATALENGTH(@DT1)
datetime
和之间的区别在这里datetime2
得到了很好的解释。
在这个练习中,我创建一个临时表,用于测试目的,并与999不同的填充它random dates
从01-jan-2019
今天(23-july-2019
)
然后按顺序将毫秒数从1设置为999
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOEXEC OFF
IF OBJECT_ID ('TEMPDB..#T1') IS NOT NULL
DROP TABLE #T1
CREATE TABLE #t1(the_date DATETIME2(3) NOT NULL PRIMARY KEY CLUSTERED )
GO
-- run this 999 times - hopefully there will be no duplicates
-- SELECT 204*24*60*60 - today is 23-july-2019 - the 203rd day of the year
DECLARE @DT DATETIME2(3)
SELECT @DT = CONVERT(DATETIME2(3),
DATEADD(SECOND, ABS(CHECKSUM(NEWID()) % 17625600),
'2019-01-01'),120)
--SELECT @DT
IF NOT EXISTS( SELECT 1 FROM #T1 WHERE THE_DATE = @DT)
INSERT INTO #T1 VALUES (@DT)
GO 999
--check it out what we have
SELECT * FROM #T1
--get the date and the new date
SELECT
THE_DATE
,THE_NEW_DATE= DATEADD(MILLISECOND, ROW_NUMBER() OVER (ORDER BY THE_DATE), THE_DATE )
FROM #T1
这就是我得到的:(部分视图)