I have date that I want to increment with 1 millisecond. I am using this sql,
DECLARE @A TABLE
(
A VARCHAR(2)
)
INSERT INTO @A(A) VALUES ('a1')
INSERT INTO @A(A) VALUES ('b2')
INSERT INTO @A(A) VALUES ('a3')
INSERT INTO @A(A) VALUES ('b4')
INSERT INTO @A(A) VALUES ('a5')
INSERT INTO @A(A) VALUES ('b6')
INSERT INTO @A(A) VALUES ('a7')
INSERT INTO @A(A) VALUES ('b8')
SELECT DATEADD(millisecond, + ROW_NUMBER() OVER (ORDER BY A), '2012-11-22 15:09:24.990'),ROW_NUMBER() OVER (ORDER BY A)
FROM @A
But the result is,
2012-11-22 15:09:24.990 1
2012-11-22 15:09:24.993 2
2012-11-22 15:09:24.993 3
2012-11-22 15:09:24.993 4
2012-11-22 15:09:24.997 5
2012-11-22 15:09:24.997 6
2012-11-22 15:09:24.997 7
2012-11-22 15:09:24.997 8
which is incorrect
The
DATETIMEdatatype in SQL Server has a 3.33ms resolution.You will never be able to create
15:09:24.991and other values – what you’re seeing is expected and documented behavior.If you need greater accuracy – use the
DATETIME2datatype instead (which can handle accuracy down to 100ns).generates results:
as you wanted.
Update: if you must stick with
DATETIME– yes, you can add +3 and get these results:gives you: