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.

thanks
mgmhicksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
SET              CREATEDDATE = DATEADD(d, DATEDIFF(d, TRANSDATE, CREATEDDATE)* -1, CREATEDDATE)
WHERE     (TRANSDATE >= '04/07/2015')
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Sounds like a plan.  Thanks for the grade, good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.