Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to subtract two time in sql server?

So I don't know how to subtract two time(hh:mm:ss) in sql server.

This is my statement:

where   
 ( CONVERT(TIME(7), [EndWork], 102)) - ( CONVERT(TIME(7), [StartWork], 102)) <
 CONVERT(TIME(7), ' 8:30:00', 102)
like image 363
Mohammad Saadeh Avatar asked Apr 28 '16 06:04

Mohammad Saadeh


People also ask

How do I subtract two timestamps in SQL Server?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .

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 ) .

How do I subtract days from a timestamp in SQL?

To get yesterday's date, you need to subtract one day from today's date. Use GETDATE() to get today's date (the type is datetime ) and cast it to date . 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 .


2 Answers

Using the DATEDIFF function you will get the difference of two dates/time in Year/Month/Day/Hour/Min/Sec as you required.

Eg:- DATEDIFF ( MINUTE , startdate , enddate ) --will return the diff in minutes

like image 111
Abdul Rasheed Avatar answered Sep 19 '22 02:09

Abdul Rasheed


You can try to use the DATEDIFF function like this:

where DATEDIFF(HH,StartWork, EndWork)
like image 36
Rahul Tripathi Avatar answered Sep 23 '22 02:09

Rahul Tripathi