Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
How to subtract  this: InTime(nvarchar(20)) – OutTime(nvarchar(20))¬¬ in sql server 2005
Values will be in both of columns like 10:00, 10:30, 10:40 etc…
Posted

use this simple code it will work

SQL
declare @InTime nvarchar(10) set @InTime='10:00'
declare @OutTime nvarchar(10) set @OutTime='11:30'
select datediff(minute,convert(datetime, convert(char(10), @InTime,110)),convert(datetime, convert(char(10),@OutTime, 110)))
 
Share this answer
 
Hi friend,

this will help you...


SQL
DECLARE @A DATETIME
DECLARE @B DATETIME
DECLARE @Secs DECIMAL(18, 2)

SET @A = '2005-08-06 12:08:00.000'
SET @B = '2005-08-18 00:30:19.000'

SELECT @Secs = DATEDIFF(s, @A, @B) / 60.000000 / 60.00000 / 24.000000
select @Secs
 
Share this answer
 
Dear Dinesh,
Hope this will help you.
SQL
select DATEDIFF(day,col1,col2) AS NumberOfDays,
DATEDIFF(hour,col1,col2) AS NumberOfHours,
DATEDIFF(minute,col1,col2) AS NumberOfMinutes FROM Your_Table

select DATEDIFF(day,2012-06-19,2012-06-20) AS NumberOfDays,
DATEDIFF(hour,2012-06-19,2012-06-20) AS NumberOfHours,
DATEDIFF(minute,2012-06-19,2012-06-20) AS NumberOfMinutes FROM Your_Table


Regards,
AP
 
Share this answer
 
v2
Comments
Dinesh Ambaliya 19-Jun-12 6:05am    
But col1 and col2 has nvarchar() data types!
Hi
Try this
SQL
SELECT Format(j.Leaving_time - j.Arrival_Time, "hh:nn:ss") AS Time_Difference
FROM Technicians AS t, Jobs AS j, Tech_Allocation AS ta
WHERE j.Job_ID = ta.Job_ID     
  AND ta.Technician_ID = t.Technician_ID
ORDER BY 1 ASC;

Beware, this approach is only valid for Time_Differences less than 24 hours
This is easier way to find difference
You can also use "DateDiff" function for subtraction
DateDiff[^]
DateDiff Approach is the best way.
 
Share this answer
 
Hi ,
Check this using DATEDIFF

SQL
declare @t1 time
set @t1 ='10:00'
declare @t2 time
set @t2 ='10:30'

select DATEDIFF( MI ,@t1 ,@t2)

Best regards
M.Mitwalli
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900