Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract two time values in ms sql server 2008

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

please help

like image 389
Vyasdev Meledath Avatar asked Oct 05 '10 11:10

Vyasdev Meledath


People also ask

How do I subtract two time values in SQL?

MySQL SUBTIME() Function The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.

How do I subtract hours and minutes in SQL?

Time subtraction: result = time1 - time2 If MINUTE( TIME2 ) > MINUTE( TIME1 ) then MINUTE( RESULT ) = 60 + MINUTE( TIME1 ) - MINUTE( TIME2 ) and HOUR( TIME2 ) is incremented by 1. HOUR( RESULT ) = HOUR( TIME1 ) - HOUR( TIME2 ) .

Can you subtract datetime in SQL?

The answer is yes. The subtract operation can be performed by the subtract operator (-) as: datetime1 - datetime2: Returning a DATETIME value calculated as CONVERT(DATETIME, CONVERT(NUMERIC(18,9),datetime1) - CONVERT(NUMERIC(18,9),datetime2)).

How do I subtract days from a timestamp in SQL?

In SQL Server, you can subtract or add any number of days using the DATEADD() function. The DATEADD() function takes three arguments: datepart , number , and date . Here, the value of datepart is day , because the unit of time you want to subtract is day.


2 Answers

You can use DATEDIFF():

SELECT DATEDIFF(Day, startDate, endDate)
  FROM table

SELECT DATEDIFF(Second, date, GETDATE())
  FROM table
like image 97
NullUserException Avatar answered Sep 28 '22 22:09

NullUserException


DECLARE @END TIME = '16:00:00.0000000' ,    
     @START TIME = '01:00:00.0000000'
     SELECT convert(TIME,dateadd(ms,DateDiff(ss, @START, @END )*1000,0),114)
like image 26
OSAMA ORABI Avatar answered Sep 28 '22 21:09

OSAMA ORABI