Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL: How to update just date part of datetime field?

In SQL Server 2008 I need to update just the date part of a datetime field.

In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?

like image 276
Marc Avatar asked Aug 26 '09 09:08

Marc


People also ask

How to update only the month part of a SQL Server date?

Update only the MONTH part of a SQL Server date using the DATEADD() function. Similarly, if you have been asked to change the month of the start_date column to specific month for few records of the table based on some condition, then you can use the dateadd() function to do the same. Use below script to change the only day part of the start_date.

How to return date part only from a SQL Server datetime datatype?

How to Return Date Part Only from a SQL Server Datetime datatype Example 1 In this SQL Server example, first, we are going to declare a DateTime variable, and also use the GETDATE() function. Next, we are going to use the CONVERT, CAST , DATEADD , and DATEPART functions to extract the date part only from a SQL server Datetime Datatype .

What is a date in SQL Server?

In SQL Server 2008, Microsoft introduced a new data-type “date”. This data type will store only the date part (day, month and year).

How to extract the date part from datetime in SQL Server?

In SQL Server 2008, Microsoft introduced a new data-type “date”. This data type will store only the date part (day, month and year). You can use the date data-type along with CONVERT or CAST to extract the date part from DateTime and DateTime2. 1.


1 Answers

One way would be to add the difference in days between the dates to the old date

UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,@newDate),<datetime>)
WHERE ...
like image 165
Ed Harper Avatar answered Oct 19 '22 07:10

Ed Harper