• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 46
  • Last Modified:

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?
0
Dale Fye
Asked:
Dale Fye
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
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
 
NorieVBA ExpertCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Dale FyeAuthor 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
 
NorieVBA ExpertCommented:
Dale

Can you give some examples that didn't work as expected?
0
 
Dale FyeAuthor 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
 
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 FyeAuthor Commented:
Thanks, Scott and Mark.

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

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now