Merge two fields one date, one time into a date/time value

Working on a clients database.  Table contains columns:

ContactDate datetime with 00:00:00 as the time component
ContactStartTime datetime with 1899-12-30 as the date component

How can I add these together in SQL Server to get a valid date/time value?
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
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.

Dustin SaundersDirector of OperationsCommented:
You just need to add the two together.

SELECT CAST([datestamp] AS datetime) + CAST([timestamp] AS datetime) FROM table

Open in new window

0
Dustin SaundersDirector of OperationsCommented:
*note i edited, didn't see that they weren't both datetime.  Just cast them as such.
If they are both datetime, just add.
0
NorieAnalyst Assistant Commented:
Sounds like you might already have 2 datetime fields, and if you do then you should be able to just add them, or you could try this.

CAST(ContactDate AS DATETIME) + CAST(ContactStartTime AS DATETIME)
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks,

I assumed that would work, but the result did not work properly when I tried it.

I'll try it again tomorrow.
0
NorieAnalyst Assistant Commented:
Dale

Can you give some examples that didn't work as expected?
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
on my way out the door.  Will take a look at it in the AM and get back to you.
0
Scott PletcherSenior DBACommented:
Both will contain a date, but you only want to add the time portion of the "ContactStartTime" column:

SELECT DATEADD(SECOND, DATEDIFF(SECOND, CAST(ContactStartTime AS date), ContactStartTime), ContactDate)

You might be able to shorten the code by CASTing time to varchar instead, but that would perform much worse than sticking to purely mathematical calcs.
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
Mark WillsTopic AdvisorCommented:
and you dont mention origins - if datasource has a high precision, then might need datetime2 datatypes to work with. I acknowledge you say they are datetime, but double check, MS are now recommending datetime2, date, time, timeoffset, so moving forward, probably need to follow / use the formal date /time routines.

So, Scott is quite correct, the best way, is to add seconds (or milliseconds)
declare @d datetime2 = '1899-12-30'
declare @t datetime2 = '12:34:56.12513'

select @t as [Meant to be Time]                                   -- but has default 1900-01-01 as date for display

select convert(datetime, format(@d,'yyyy-MM-dd ')+format(@t,'hh:mm:ss.fff'))  -- this works converting to any datetime but note rounding on datetime

select dateadd(ms, datediff(ms, CAST(@t AS date), @t), @d)        -- this works using milliseconds 

select dateadd(s, datediff(s, CAST(@t AS date), @t), @d)           -- this works but note truncation of seconds

select cast(@d as datetime)+cast(@t as datetime)            -- will also work - but will depend on datatypes (cannot add datetime2 or date like that)

Open in new window

Unless they are varchar
declare @d varchar(20) = '1899-12-30'
declare @t varchar(20) = '12:34:56.125'

select convert(datetime, @d+' '+@t)

Open in new window

or 100% sure they are are nothing other than both datetime then you can
declare @d datetime = '1899-12-30'
declare @t datetime = '12:34:56.125'

select @d+@t

Open in new window

By way of comparison, you cant add 1 to DATE, DATETIME2 but can to DATETIME, you now have to use dateadd(), or, cast(... as datetime) to make sure of your date/time based handling....
0
Scott PletcherSenior DBACommented:
or 100% sure they are are nothing other than both datetime then you can

declare @d datetime = '1899-12-30'
declare @t datetime = '12:34:56.125'
select @d+@t

As I noted above, no, you can't:

declare @d datetime = '2016-04-01'
declare @t datetime = '1899-12-30 12:34:56.125'
select @d+@t

For the "simple" add to work, the date on the time needs to be 1900-01-01, i.e., date 0.  Then that date won't affect the result, since 0 will be added to days.

But when the date part of the time column is -2 (i.e. 1899-12-30), attempting to add it directly will back the date up 2 days.
0
Mark WillsTopic AdvisorCommented:
Scott,

If you change the declaratives to contain other facets of date or time that it will be wrong. The two fields, one is date only and the other is time only. I can also make your code return a result inconsistent with the time aspect.

I had originally endorsed your post supporting your claim to do it 'the right way'
0
Scott PletcherSenior DBACommented:
Yes.  If an actual column of type "time" can't be used, I prefer to make sure that I use only the time portion of the column, ignoring the date completely to be absolutely safe.
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks, Scott and Mark.

Appreciate the assistance.
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.