日期和时差格式 [英] date & time difference format

查看:130
本文介绍了日期和时差格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录/退出系统,可以计算员工的总工时。我的问题是日期和时间差的格式化分钟。



示例:



如果用户登录上午8:00 并注销 6:30 PM ,总数应 10.50 但我的SQL输出 10小时30分钟。我知道公式(它应该 30分钟/ 60 = 50 )但我不知道我应该在哪里将红利(/ 60)放在我的SQL查询上。(见下面我的SQL代码)



SQL代码:



  SELECT  *,CAST(Datediff(HOUR, 0 ,logout_time  -  login_time)+ 
Cast(Datediff(MINUTE, 0 ,logout_time - login_time) -
(Datediff(HOUR, 0 ,logout_time - login_time)* 60 AS DECIMAL )/ 100 AS 十进制 18 2 )) AS [TotalHrs_Worked]
FROM table_DTR
< span class =code-keyword> WHERE CONVERT VARCHAR 10 ),log_date, 101 BETWEEN ' 09/01/2014' AND ' 11/30/2014'

解决方案

检查这个





 声明  @ date1 日期时间= '  2014年12月15日08:00 AM' 
声明 @ date2 日期时间= ' 2014年12月15日06:30 PM'

选择日期IFF(MINUTE, @ date1 @ date2 )/ 60. 0


忽略小时:仅在几分钟内完成差异,并将其转换为浮点小时,您将得到结果。

  SELECT  *,CAST(DATEDIFF(分钟,login_time,logout_time) AS   DECIMAL )/  60   AS 工作 FROM  MyTable 


试试..

  SELECT  *,CAST((Datediff(MINUTE,login_time,logout_time)/ 60) as   decimal  18  2 )) AS  [TotalHrs_Worked] 
< span class =code-keyword> FROM table_DTR
WHERE CONVERT VARCHAR 10 ),log_date, 101 BETWEEN ' 09/01/2014' AND ' 11/30/2014'


I have a Login/Logout system that compute the total hours work of the employees. My problem is the formatting of the date&time difference for minutes.

Example:

if user logged-in 8:00 AM and logged-out 6:30 PM, the total should be 10.50 but my SQL output is 10 hrs and 30 mins. I know the formula(it should be 30mins/60 = 50) but I didn't know where should I put that Dividend(/60) on my SQL Query.(see below my SQL Code).

SQL Code:

SELECT *,CAST(Datediff(HOUR, 0, logout_time - login_time) +
         Cast(Datediff(MINUTE, 0, logout_time - login_time) -
             (Datediff(HOUR, 0, logout_time - login_time) * 60 ) AS DECIMAL) / 100 AS Decimal(18,2)) AS [TotalHrs_Worked]
FROM table_DTR
WHERE CONVERT(VARCHAR(10),log_date, 101) BETWEEN '09/01/2014' AND '11/30/2014'

解决方案

check this


Declare @date1 Datetime='Dec 15 2014 08:00 AM'
Declare @date2 Datetime='Dec 15 2014 06:30 PM'

select DATEDIFF(MINUTE,@date1,@date2)/60.0


Ignore the hours: do the difference in minutes only, and convert that to floating point hours and you will get your result.

SELECT *, CAST(DATEDIFF(minute, login_time, logout_time) AS DECIMAL) / 60 AS Worked FROM MyTable


try..

SELECT *,CAST((Datediff(MINUTE,login_time,logout_time)/60) as decimal(18,2)) AS [TotalHrs_Worked]
    FROM table_DTR
    WHERE CONVERT(VARCHAR(10),log_date, 101) BETWEEN '09/01/2014' AND '11/30/2014'


这篇关于日期和时差格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆