How to add back a time component to a date


I'm working on creating a view.  IThe underlying table has many fields with four fields of note
StartDate - Datetime
StartHour - Decimal
DueDate - Datetime
DueHour - Decimal

When the database was designed, it appears that instead of a datetime field containing both the time and date components, only the date data was entered, so dates would appear and be stored as 2011-08-26 00:00:00:0000.  The time was entered as a decimal number so for example a time of 23:30 would be entered as 23.5
I want to add the corresponding time back to the date field so that I can calculate the corresponding time difference in minutes between the (StartDate + Startime) and (DueDate + DueTime) via the datediff function.  I can already convert the decimal time component to hh:mm:ss via CONVERT(VARCHAR(8), Dateadd(minute, [duehour]*60,0),108) AS HMS.  I'm just not sure how to add it to the date time component as a calculated field so that it would appear as a datetime 2011-08-26 23:30:00:000.  This database is running on SQL Server 2005
Juan VelasquezAsked:
Who is Participating?

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

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.

Juan VelasquezAuthor Commented:
I think I may have figured it out
[duedate] + CONVERT(DateTime, Dateadd(minute, [duehour]*60,0),108)  AS NewDueDate
Leo TorresSQL DeveloperCommented:
Not sure how you mean add

but it should be similar to this
Select dateadd(hour,3,getdate())

Open in new window

Juan VelasquezAuthor Commented:
Here is the sql string that I came up with
	  ,DATEDIFF(minute,[startdate] + CONVERT(DateTime, Dateadd(minute, [starthour]*60,0),108),[duedate] + CONVERT(DateTime, Dateadd(minute, [duehour]*60,0),108)) AS 'Capacity'
  FROM [mfgtest803].[dbo].[joboper] 

Open in new window

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.

Leo TorresSQL DeveloperCommented:
I guess my question is how much time are you adding and what are you adding to? That why I can do the sql math.
Juan VelasquezAuthor Commented:
I appreciate the help.  Since the start time is a datetime datatype and the converted start hour is now also a date time datatype, I was able to add them.  In this case I thing I can use either the sql date add or the + operator
PortletPaulEE Topic AdvisorCommented:
Instead of the zero as third parameter  in this:

      Dateadd(minute, [duehour]*60,0)

use the relevant date column

      Dateadd(minute, [StartHour]*60,[StartDate])

      Dateadd(minute, [duehour]*60,[DueDate])

try to avoid using varchar conversions for date calculations, using varchars is slower than date functions

+edit2 and the datediff would be like this.  

DATEDEFF(minute,  Dateadd(minute, [StartHour]*60,[StartDate]) ,  Dateadd(minute, [duehour]*60,[DueDate]) )

but you may need to reverse the order of those 2 dates

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
Juan VelasquezAuthor Commented:
Thanks for the help.  I will implement your suggestion
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 2005

From novice to tech pro — start learning today.