DATEDIFF不返回整数值T-SQL

时间:2021-12-28 22:10:18

I am trying to see if how much time it took to insert some values into a table in SQL Server 2012.

我试图看看在SQL Server 2012中将一些值插入表中需要多少时间。

This how I am trying to do:

这是我试图做的:

DECLARE @starttime DATETIME
DECLARE @timetook DATETIME

CREATE TABLE MyTable (id INT, name CHAR(10));

SET @starttime = SYSDATETIME()

INSERT INTO MyTable (id, name) 
VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe'),
       (4, 'Boby'), (5, 'Peters'), (6, 'Joey'),
       (7, 'Bobs'), (8, 'Petery'), (9, 'Joes');

SET @timetook = DATEDIFF(ss, @starttime, SYSDATETIME())

SELECT @timetook

This returns 1900-01-01 00:00:00.000. According to the datediff docs, it should be 00:00:00.000 or an integer value. Since I am taking the seconds, I should get the number in seconds. (For this example it should be 0 but without 1900-01-01)

返回1900-01-01 00:00:00.000。根据datediff docs,它应该是00:00:00.000或整数值。因为我花了几秒钟,所以我应该在几秒钟内得到这个数字。 (对于此示例,它应为0但没有1900-01-01)

What am I missing here? Any suggestion would be appreciated.

我在这里想念的是什么?任何建议将不胜感激。

1 个解决方案

#1


3  

Declaring @timetook as datetime will give you a datetimeresult. And the datetime = 0 is specifically 1900-01-01 00:00:00.000.

将@timetook声明为datetime会给你一个datetimeresult。 datetime = 0具体为1900-01-01 00:00:00.000。

declare @starttime datetime
declare @timetook int --Issue was this declaration as datetime

CREATE TABLE MyTable (id int, name char(10));

set @starttime = SYSDATETIME()

INSERT INTO MyTable (id, name) VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe'),
                                      (4, 'Boby'), (5, 'Peters'), (6, 'Joey'),
                                      (7, 'Bobs'), (8, 'Petery'), (9, 'Joes');
set @timetook = DATEDIFF(ss, @starttime, SYSDATETIME())

select @timetook

By declaring @timetook as int, you will get the result you are looking for: 0

通过将@timetook声明为int,您将获得您要查找的结果:0

#1


3  

Declaring @timetook as datetime will give you a datetimeresult. And the datetime = 0 is specifically 1900-01-01 00:00:00.000.

将@timetook声明为datetime会给你一个datetimeresult。 datetime = 0具体为1900-01-01 00:00:00.000。

declare @starttime datetime
declare @timetook int --Issue was this declaration as datetime

CREATE TABLE MyTable (id int, name char(10));

set @starttime = SYSDATETIME()

INSERT INTO MyTable (id, name) VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe'),
                                      (4, 'Boby'), (5, 'Peters'), (6, 'Joey'),
                                      (7, 'Bobs'), (8, 'Petery'), (9, 'Joes');
set @timetook = DATEDIFF(ss, @starttime, SYSDATETIME())

select @timetook

By declaring @timetook as int, you will get the result you are looking for: 0

通过将@timetook声明为int,您将获得您要查找的结果:0