visual-studio 使用 GetDate() 函数将日期时间值插入 SQL Server (2005) 表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3018633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:06:12  来源:igfitidea点击:

Insert a datetime value with GetDate() function to a SQL server (2005) table?

visual-studiotsql

提问by David.Chu.ca

I am working (or fixing bugs) on an application which was developed in VS 2005 C#. The application saves data to a SQL server 2005. One of insert SQL statement tries to insert a time-stamp value to a field with GetDate() TSQL function as date time value.

我正在处理(或修复错误)在 VS 2005 C# 中开发的应用程序。该应用程序将数据保存到 SQL Server 2005。其中一个插入 SQL 语句尝试将时间戳值插入到具有 GetDate() TSQL 函数作为日期时间值的字段中。

Insert into table1 (field1, ... fieldDt) values ('value1', ... GetDate());

The reason to use GetDate() function is that the SQL server may be at a remove site, and the date time may be in a difference time zone. Therefore, GetDate() will always get a date from the server. As the function can be verified in SQL Management Studio, this is what I get:

使用 GetDate() 函数的原因是 SQL Server 可能在删除站点,并且日期时间可能在不同的时区。因此,GetDate() 将始终从服务器获取日期。由于该功能可以在 SQL Management Studio 中进行验证,这就是我得到的:

SELECT GetDate(), LEN(GetDate());
-- 2010-06-10 14:04:48.293   19

One thing I realize is that the length is not up to the milliseconds, i.e., 19 is actually for '2010-06-10 14:04:48'. Anyway, the issue I have right now is that after the insert, the fieldDt actually has a date time value up to minutes, for example, '2010-06-10 14:04:00'. I am not sure why. I don't have permission to update or change the table with a trigger to update the field.

我意识到的一件事是长度不能达到毫秒,即 19 实际上是 '2010-06-10 14:04:48'。无论如何,我现在遇到的问题是,在插入之后,fieldDt 实际上有一个长达几分钟的日期时间值,例如,'2010-06-10 14:04:00'。我不知道为什么。我无权使用触发器更新或更改表来更新字段。

My question is that how I can use a INSERT T-SQL to add a new row with a date time value ( SQL server's local date time) with a precision up to milliseconds?

我的问题是如何使用 INSERT T-SQL 添加具有日期时间值(SQL 服务器的本地日期时间)的新行,精度可达毫秒?

回答by George Mastros

Check your table. My guess is that the FieldDT column has a data type of SmallDateTime which stores date and time, but with a precision to the nearest minute. If my guess is correct, you will not be able to store seconds or milliseconds unless you change the data type of the column.

检查你的桌子。我的猜测是 FieldDT 列有一个 SmallDateTime 数据类型,它存储日期和时间,但精度到最接近的分钟。如果我的猜测是正确的,除非您更改列的数据类型,否则您将无法存储秒或毫秒。

回答by Panagiotis Kanavos

I would guess that you are not storing the GetDate() value in a DateTime field. If you store the value in a datetime field you will get the maximum precision allowed by the DateTime type. Additionally, DateTime is a binary type (a double actually) so 19 means 19 bytes, not 19 characters.

我猜您没有将 GetDate() 值存储在 DateTime 字段中。如果您将值存储在日期时间字段中,您将获得 DateTime 类型允许的最大精度。此外,DateTime 是一种二进制类型(实际上是一个双精度型),因此 19 表示 19 个字节,而不是 19 个字符。

Try to create a simple table with a Datetime field like this

尝试使用这样的日期时间字段创建一个简单的表

CREATE TABLE [dbo].[DateTable](
[DateField] [datetime] NOT NULL
)

And add a date with

并添加一个日期

insert into datetable (datefield) values(getdate())

When you execute a select you will get back a value including milliseconds. The following query

当您执行选择时,您将返回一个包含毫秒的值。以下查询

select *  from datetable

returns

回报

2010-06-11 00:38:46.660

2010-06-11 00:38:46.660

回答by Neil Moss

As you're on SQL 2005, don't forget the getutcdate() function to ensure that, regardless of where your servers are actually located, you have a constant time reference.

当您使用 SQL 2005 时,不要忘记 getutcdate() 函数以确保无论您的服务器实际位于何处,您都有一个恒定的时间参考。

Imagine, you have the server in the UK in winter (i.e. GMT+0), and save a record at 10:30am. You then cut over to a SQL server hosted in California (GMT+8) and 8 hours later save another record.

想象一下,你在英国有服务器在冬天(即 GMT+0),并在上午 10:30 保存一条记录。然后,您切换到位于加利福尼亚 (GMT+8) 的 SQL 服务器,并在 8 小时后保存另一条记录。

Using getdate(), both saves record the same time "10:30:00". Using getutcdate(), the first save records at "10:30:00", the second save records "18:30:00".

使用 getdate(),两者都保存记录同一时间“10:30:00”。使用getutcdate(),第一次保存记录在“10:30:00”,第二次保存记录“18:30:00”。

Not really answering the question, but important in your circumstances.

不是真正回答问题,但在您的情况下很重要。

回答by Nirav Ravani

You can use like this in procedure and If there is no procedure use only getdate().

您可以在程序中使用这样的方法,如果没有程序,则仅使用getdate()

insert into [dbo].[Tbl_User] (UserId,Uvendoremail,UAddress,Ddob,DMobile, DEmail,DPassword,DAddress,CreatedDate) values (@userid,@vendoremail@address,@dob,@mobile,@email,@dpassword,@daddress,getdate())

插入 [dbo].[Tbl_User] (UserId,Uvendoremail,UAddress,Ddob,DMobile, DEmail,DPassword,DAddress, CreatedDate) 值 (@userid,@vendoremail@address,@dob,@mobile,@email,@dpassword, @daddress, getdate())

回答by Vishal

Maybe this would work instead of getdate - SYSDATETIME() look here if you can find what you need - http://msdn.microsoft.com/en-us/library/ms188383.aspx

也许这可以代替 getdate - SYSDATETIME() 看看这里,如果你能找到你需要的 - http://msdn.microsoft.com/en-us/library/ms188383.aspx