How to subtract two time values in sql server 2008.I am using time variables in stored procedure.
please help
MySQL SUBTIME() Function The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.
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 ) .
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)).
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.
You can use DATEDIFF()
:
SELECT DATEDIFF(Day, startDate, endDate)
FROM table
SELECT DATEDIFF(Second, date, GETDATE())
FROM table
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With