views:

38

answers:

1

Hai all,

How to subtract two time values in sql server 2008.I am using time variables in stored procedure.

please help

+3  A: 

You can use DATEDIFF():

SELECT DATEDIFF(Day, startDate, endDate)
  FROM table

SELECT DATEDIFF(Second, date, GETDATE())
  FROM table
NullUserException
@NullUserException: for using DATEDIFF() varible should be in datetime?
Vyas
@Vyas What datatype do you have?
NullUserException
@NullUserException:datatype is `time`
Vyas
@Vyas - It works for `time` datatype - e.g. `SELECT DATEDIFF(Second, CAST('11:24:18.3500000' as time),CAST('12:24:18.3500000' as time))`
Martin Smith
According to MSDN documentation (found at http://msdn.microsoft.com/en-us/library/ms189794.aspx) the last two parameters are "an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value."
Emil Badh