GeekInterview.com
Series: Subject:
Question: 79 of 80

I have a table in sql server with two fields named 'start_time' and 'stop_time'. both fields have datatype set as 'datetime'. now I want to subtract the 'start_time' from 'stop_time'? I have tried datediff() fuction,but no use. I want the correct outpput of date as well as time .
Need help as early as possible.

Asked by: Interview Candidate | Asked on: Nov 16th, 2006
Showing Answers 1 - 6 of 6 Answers
sathyavision

Answered On : Nov 21st, 2006

View all answers by sathyavision

Hi...

Try this

select convert(varchar, datediff(hour, start_time, stop_time) / 24)+
' Days ' +
convert(varchar, datediff(hour, start_time, stop_time) % 24) + ' Hrs ' 

from tbl_name

  
Login to rate this answer.
MSenthil_DBA

Answered On : Apr 9th, 2007

View all answers by MSenthil_DBA

Hi,

I don't know the problem, try this one

select datediff(hh, start_time, stop_time)  from table

gives difference hours between two time

  
Login to rate this answer.
goldjoy

Answered On : May 11th, 2007

declare @starttime datetime
declare @timediff bigint
set @starttime='11-May-2007'
SELECT @timediff = DATEDIFF(millisecond, @starttime, getdate())
select @timediff%1000 --for ml sec part
select (@timediff/1000)%60 --for sec part
select (@timediff/(1000*60))%60 --for min part
select (@timediff/(1000*3600))%60 --for hour part

and concatenate the results

  
Login to rate this answer.
avbhaskar

Answered On : Jun 2nd, 2008

View all answers by avbhaskar

Try this example:

declare @startdatetime datetime
declare @stopdatetime datetime
declare @diffdays int
declare @diffhours int
declare @diffmins int
-- assumption is startdate <= stopdate always
SET @startdatetime = '2008-06-01 00:00:00.00'
SET @stopdatetime = '2008-06-02 11:35:43.793'

SET @diffdays = DATEDIFF(dd,@startdatetime,@stopdatetime)
SET @diffhours = (DATEDIFF(hh,@startdatetime,@stopdatetime) - @diffdays*24)
SET @diffmins = (DATEDIFF(mi,@startdatetime,@stopdatetime)- @diffhours*60-@diffdays*24*60)

SELECT @startdatetime 'start-date', @stopdatetime 'stop-date',
CONVERT(VARCHAR(4), @diffdays) + ' DAY(S) ' + CONVERT(VARCHAR(4), @diffhours) + ' HOUR(S) ' + CONVERT(VARCHAR(4), @diffmins)
+ ' MINUTE(S) ' as 'runtime'

  
Login to rate this answer.
swathi660

Answered On : Jan 15th, 2009

View all answers by swathi660

select starttime,stoptime,datediff(day,starttime,stoptime) as newtime from date.try this...........

  
Login to rate this answer.
satheyaraaj

Answered On : Jul 9th, 2011

View all answers by satheyaraaj

select DATEDIFF(MI,FromTime,ToTime) From ShiftMaster  -- between no of minutes


select DATEDIFF(HH,FromTime,ToTime) From ShiftMaster -- between no of Hours


  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.