如何在sql server中找到两次的差异 [英] How to find differences of two time in sql server
问题描述
我有一个Windows窗体,为所有用户提供time_in和time_out。 Time_in和time_out也是我的sql数据库中的表。我想找工作时间,可以计算为time_out - time_in。 Time_in和time_out是时间格式(hh:mm:ss)。如何找工作时间?我试过的代码不起作用,因为数据变量类型。但无论如何我无法使用int数据类型。
string time_in = databaseTimeIn;
string time_out = databaseTimeOut;
string workhour = time_out-time_in;
不要将字符串用于日期和时间数据!如果您使用的是SQL Server,可以直接操作DATETIME值,也可以使用DATEDIFF。
http://msdn.microsoft.com/en-us/library/ms189794.aspx [ ^ ]
hi,
计算你需要以HH格式转换in_time和out_time所需的工作时间,所以使用公式in_time为HH + mm / 60 + ss / 60/60即07:23:55 - >> 07 + 23/60 + 55/60/60 = 7.39小时
之后将工作时间计算为out-in的差异
< blockquote>使用
DATEDIFF(datepart,startdate,enddate)
例如:SELECT DATEDIFF(day,'2008-06-05', '2008-08-05')AS DiffDate
ans:61
I have a windows form that gives me time_in and time_out for all the users. Time_in and time_out are also tables in my sql database. I'd like to find work hours which can be calculated as time_out - time_in. Time_in and time_out are time format (hh:mm:ss). How to find the work hour ? The code that I tried doesn't work becuase of the data variable type. But I couldn't do this with the int data type anyway.
string time_in = databaseTimeIn; string time_out = databaseTimeOut; string workhour = time_out-time_in;
Don't use strings for date and time data! If you are using SQL Server, DATETIME values can be operated on directly, or use DATEDIFF.
http://msdn.microsoft.com/en-us/library/ms189794.aspx[^]
hi,
to calculate Work Hours you need to convert in_time and out_time in HH format so use the formula in_time as HH+mm/60+ss/60/60 i.e 07:23:55-->>07+23/60+55/60/60=7.39 hrs
after that calculate Work Hours as diff of out-in
use
DATEDIFF(datepart,startdate,enddate)
eg: SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
ans:61
这篇关于如何在sql server中找到两次的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!