Marc
Marc

Reputation: 9282

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?

Upvotes: 8

Views: 9665

Answers (1)

Ed Harper
Ed Harper

Reputation: 21495

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 ...

Upvotes: 14

Related Questions