how to get time from datetime field and merge new date

I have a table with 2 fields.  A date field and a datetime field.    What I need to do is replace the date within the datetime field and keep the time.  The date of course would be the date in the other field.  Is there any what to do that within sql transactions.

Who is Participating?
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
There's probably a more graceful way to pull this off, but the below code uses DATEDIFF to get the difference between two dates, then DATEADD to add those days to the original date, leaving the time intact.

Declare @dt datetime = GETDATE()
Declare @dt_new datetime = '2016-01-01'
SELECT DATEADD(d,  DATEDIFF(DAY, @dt, @dt_new), @dt)

Open in new window

Spell out for us the mechanics of what your table looks like, and if the date we're talking about is going to be the same date for all rows vs. a different date.
mgmhicksAuthor Commented:
Thank  you Jim that seemed to work.  Here is what the code looks like had to do a *-1 to make it work, either way I set the fields.

UPDATE    transact
WHERE     (TRANSDATE >= '04/07/2015')
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Sounds like a plan.  Thanks for the grade, good luck with your project.  -Jim
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.