How to add back a time component to a date

Hello,

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

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

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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.
0
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
0
PortletPaulfreelancerCommented:
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])

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

From novice to tech pro — start learning today.

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.