I need to add the date part of GetDate()
to the time part of a stored DateTime
.
Naively I thought I could do the following:
declare @testTime DateTime = '2013-04-23 13:55:06'
select cast(getdate() as date) + cast(@testTime as time)
but this gives the following error:
Operand data type date is invalid for add operator.
I did some more research and have come up with three possible ways of doing the calculation:
declare @testTime DateTime = '2013-04-23 13:55:06'
select dateadd(dd, 0, DATEDIFF(dd, 0, GetDate())) + cast(@testTime as time)
select cast(cast(GetDate() as date) as datetime) + cast(@testTime as time)
select dateadd(day, datediff(day, @testTime, GetDate()), @testTime)
All three produce the same result but I'm not 100% happy about implementing any of them, though the third way seems to be a better way - though I have no evidence for this.
Which (if any) of the three is the most efficient?
Is there a better way?