如何使用日期时间选择器计算时间不足?

时间:2017-01-18 08:17:32

标签: sql-server vb.net datetimepicker

我有这段代码:

 Dim cmd3 As SqlCommand
 Dim rxd As SqlDataReader
 Dim nub As String = "select sum(case when datediff(minute, '09:00:00', [Time_In]) > 0 then datediff(minute, '09:00:00', [Time_In]) else 0 end) from timelogTB where EmpNo= @find and Date_ between @dtpFrom and @dtpTo "
        cmd3 = New SqlCommand(nub)
        cmd3.Connection = conn

        With cmd3
            .Parameters.AddWithValue("@find", "EmpNo")
            .Parameters.Add(New SqlParameter("@dtpFrom", dtpFrom.Value.ToString))
            .Parameters.Add(New SqlParameter("@dtpTo", dtpTo.Value.ToString))
            .Parameters("@find").Value = Trim(comboxEmpno.Text)
        End With
        rxd = cmd3.ExecuteReader()

        If rxd.Read Then
            txtLate.Text = Trim(rxd.GetValue(0))
        End If

此代码计算员工的迟到。任何人都可以给我提示如何计算员工的工作时间。让我们说他们的时间是在上午9点之前,然后他们的超时时间是下午6点。然后1名员工在下午5:45超时,我将如何计算15分钟作为员工的工作时间。记录将从timelogTB中提取,其中time-in和time-out列是时间数据类型,而日期是日期数据类型。我正在使用sql-server 2008.进入和超时的过程来自表单1,在这种形式中有一个进入和超时按钮来节省时间。在表单2中,我想像上面的问题一样计算员工的工作时间。

This image is the Time-in & Time-out window

While the second image is the payments window.

我首先发布的代码是计算员工的迟到,让我们说如果他/她进入上午9:04将会计算4分钟。所以关于时间不足,我相信只需稍作修改即可。

2 个答案:

答案 0 :(得分:0)

首先,您应该将您的输入和输出时间值存储为DateTime值并删除日期列,否则您必须将2个时间值加入日期字段以进行任何日期计算。这只会让你的代码变得更加复杂。

您的问题仍然有点不清楚,我已经为您做了一些基本的计算,所以我会发布我的内容,希望它可以帮助您找到最终解决方案。

在SQL Server中运行以下代码,它使用带有虚拟数据的临时表,因此不会影响您的数据。

CREATE TABLE #timelogTB
    (
      EmpNo INT ,
      WorkDate DATE ,
      Time_In TIME ,
      Time_Out TIME
    );

INSERT  INTO #timelogTB
        ( EmpNo, WorkDate, Time_In, Time_Out )
VALUES  ( 1, '20170101', '09:00:00', '18:00:00' ),
        ( 1, '20170102', '09:00:00', '17:45:00' ),
        ( 1, '20170103', '09:00:00', '17:55:00' ),
        ( 1, '20170104', '08:50:00', '18:00:00' ),
        ( 2, '20170101', '09:00:00', '18:00:00' ),
        ( 2, '20170102', '09:00:00', '17:30:00' ),
        ( 2, '20170103', '09:00:00', '17:35:00' ),
        ( 2, '20170104', '09:00:00', '18:00:00' );

SELECT  * ,
        DATEDIFF(MINUTE,
                 CAST(CAST(WorkDate AS VARCHAR(10)) + ' '
                 + CAST(Time_In AS VARCHAR(10)) AS DATETIME),
                 CAST(CAST(WorkDate AS VARCHAR(10)) + ' '
                 + CAST(Time_Out AS VARCHAR(10)) AS DATETIME)) TotalMinutesWorked ,
        540 - DATEDIFF(MINUTE,
                       CAST(CAST(WorkDate AS VARCHAR(10)) + ' '
                       + CAST(Time_In AS VARCHAR(10)) AS DATETIME),
                       CAST(CAST(WorkDate AS VARCHAR(10)) + ' '
                       + CAST(Time_Out AS VARCHAR(10)) AS DATETIME)) TotalMinutesShort
FROM    #timelogTB
WHERE   --EmpNo = 1 AND
        WorkDate BETWEEN '20170101' AND '20170105';

DROP TABLE #timelogTB;

此代码检查员工的时间为540分钟(9小时),即9:00至18:00之间的时间。

答案 1 :(得分:0)

这是一个使用公用表表达式

的示例

<强>设置

import { Program } from "./Program"; 

<强>查询

使用CTE查询员工(包括一名上班迟到的员工)每天的总时间:

Program.Main();

<强>结果

CREATE TABLE #timelogTB
    (
      EmpNo INT ,
      WorkDate DATE ,
      Time_In TIME ,
      Time_Out TIME
    );

INSERT  INTO #timelogTB
        ( EmpNo, WorkDate, Time_In, Time_Out )
VALUES  ( 1, '20170101', '09:10:00', '18:00:00' ),
        ( 1, '20170102', '09:00:00', '17:45:00' ),
        ( 1, '20170103', '09:00:00', '17:55:00' ),
        ( 1, '20170104', '08:50:00', '18:00:00' ),
        ( 2, '20170101', '09:00:00', '18:00:00' ),
        ( 2, '20170102', '09:00:00', '17:30:00' ),
        ( 2, '20170103', '09:00:00', '17:35:00' ),
        ( 2, '20170104', '09:00:00', '18:00:00' );