DateTime2(7)给出0填充值

时间:2022-04-16 08:24:33

I want to save date in datetime2(7) format in my application's database. I tried this:

我想在我的应用程序的数据库中以datetime2(7)格式保存日期。我试过这个:

@CreatedDate DateTime2(7)= getutcdate();

Then i found the value was this:

然后我发现价值是这样的:

2016-08-02 15:02:14.6870000

2016-08-02 15:02:14.6870000

I want 2016-08-02 15:02:14.6871345 format.

我想要2016-08-02 15:02:14.6871345格式。

What should i do?

我该怎么办?

1 个解决方案

#1


4  

GETUTCDATE() returns a DATETIME value so will never have that sort of resolution.

GETUTCDATE()返回DATETIME值,因此永远不会有那种分辨率。

Instead try SYSUTCDATETIME() as this does return a DATETIME2 with a precision of 7 digits.

而是尝试SYSUTCDATETIME(),因为这会返回精度为7位的DATETIME2。

For example:

例如:

SELECT GETUTCDATE() AS UtcDate,
       SYSUTCDATETIME() AS SysUtcDate

Will give something like this:

会给出这样的东西:

╔═════════════════════════╦═════════════════════════════╗
║ UtcDate                 ║ SysUtcDate                  ║
╠═════════════════════════╬═════════════════════════════╣
║ 2016-08-02 09:49:54.347 ║ 2016-08-02 09:49:54.3486893 ║
╚═════════════════════════╩═════════════════════════════╚

#1


4  

GETUTCDATE() returns a DATETIME value so will never have that sort of resolution.

GETUTCDATE()返回DATETIME值,因此永远不会有那种分辨率。

Instead try SYSUTCDATETIME() as this does return a DATETIME2 with a precision of 7 digits.

而是尝试SYSUTCDATETIME(),因为这会返回精度为7位的DATETIME2。

For example:

例如:

SELECT GETUTCDATE() AS UtcDate,
       SYSUTCDATETIME() AS SysUtcDate

Will give something like this:

会给出这样的东西:

╔═════════════════════════╦═════════════════════════════╗
║ UtcDate                 ║ SysUtcDate                  ║
╠═════════════════════════╬═════════════════════════════╣
║ 2016-08-02 09:49:54.347 ║ 2016-08-02 09:49:54.3486893 ║
╚═════════════════════════╩═════════════════════════════╚