Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Update the Date without changing its Time Using SQL Server?

I have One Column in the Mytable called StockDate and its DataType is 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.

like image 476
thevan Avatar asked Oct 03 '11 04:10

thevan


People also ask

How can get current date without time in SQL Server?

We'll use the GETDATE() function to get the current date and time. Then we'll use the CAST() function to convert the returned datetime data type into a date data type.

How can UPDATE only date in datetime column of table in SQL Server?

To update with the current date and time: UPDATE table_name SET date_field = CURRENT_TIMESTAMP; To update with a specific date value: UPDATE table_name SET date_field = 'YYYY-MM-DD HH:MM:SS.

Can we UPDATE date in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statements as per our requirement. With this article, we will learn how to Update the Date Field in SQL Server.

How do I query a date without time in SQL?

SQL FORMAT select * from Bookings where StartTime >= cast('2014-02-15' as date) and StartTime < cast('2014-02-14' as date); Show activity on this post. '2/15/2014' can be interpreted different depending on your locale. Try using the ISO date literal '2014-02-15', which is independent of the locale.


1 Answers

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.

like image 55
gbn Avatar answered Oct 06 '22 12:10

gbn