如何使用SQL Server更新日期而不更改其时间?

时间:2021-04-09 17:02:18

I have One Column in the Mytable called StockDate and its DataType is DateTime.

我在Mytable中有一个名为StockDate的列,其DataType是DateTime。

So My Table contains record as follows

所以我的表包含如下记录

    SID                StockDate
   -----              ------------
     1                 2011-10-02 09:44:41.170
     2                 2011-10-02 09:48:23.234

Here I want to update Only the Date of the StockDate as "2011-09-30". But the time should be same as it is. How to do this? Please I need all your suggestions.

在这里,我想仅将StockDate的日期更新为“2011-09-30”。但时间应该是一样的。这个怎么做?我需要你的所有建议。

3 个解决方案

#1


9  

Work out the different in whole days and subtract that...

整天锻炼出不同的东西并减去......

UPDATE
   MyTable
SET
   StockDate = DATEADD(day, DATEDIFF(day, StockDate, '20110930'), StockDate)
WHERE
   ...

Note the use of the yyyymmdd for SQL Server.

请注意yyyymmdd用于SQL Server。

#2


1  

You can use dateadd, datediff functions.

您可以使用dateadd,datediff函数。

#3


1  

http://msdn.microsoft.com/en-us/library/ms186724(v=SQL.90).aspx

http://msdn.microsoft.com/en-us/library/ms186724(v=SQL.90).aspx

You could probably use DATEDIFF and DATEADD to get yourself to 9/30/2011 without changing the time.

你可以使用DATEDIFF和DATEADD让你自己到9/30/2011而不改变时间。

#1


9  

Work out the different in whole days and subtract that...

整天锻炼出不同的东西并减去......

UPDATE
   MyTable
SET
   StockDate = DATEADD(day, DATEDIFF(day, StockDate, '20110930'), StockDate)
WHERE
   ...

Note the use of the yyyymmdd for SQL Server.

请注意yyyymmdd用于SQL Server。

#2


1  

You can use dateadd, datediff functions.

您可以使用dateadd,datediff函数。

#3


1  

http://msdn.microsoft.com/en-us/library/ms186724(v=SQL.90).aspx

http://msdn.microsoft.com/en-us/library/ms186724(v=SQL.90).aspx

You could probably use DATEDIFF and DATEADD to get yourself to 9/30/2011 without changing the time.

你可以使用DATEDIFF和DATEADD让你自己到9/30/2011而不改变时间。