使用特定时间将GetDate()转换为当前日期

时间:2014-04-02 06:27:33

标签: c# sql sql-server database visual-studio-2013

我试图找出一种方法来转换GetDate()仅从当前日期和08:15到09:00之间发送结果,此查询的目的是确切地找出哪些员工在这些时间之间登录系统,但每次我尝试更改代码以便向我发送当前日期时我都会收到错误。

private void button10_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = @"Data Source=GTX-COMSVR2\SQLEXPRESS;Initial Catalog=Att;Persist Security Info=True;User ID=**;Password=********";
    //con.Open();
    SqlDataAdapter sda = new SqlDataAdapter(" SELECT USERINFO.USERID [User ID], USERINFO.BADGENUMBER [Employee No.], USERINFO.NAME [Name], CHECKINOUT.CHECKTIME [Clock Time], CHECKINOUT.CHECKTYPE [I/O], CHECKINOUT.SENSORID [Sensor ID] FROM USERINFO, CHECKINOUT WHERE USERINFO.USERID = CHECKINOUT.USERID and CHECKINOUT.CHECKTYPE = 'I' and CHECKINOUT.SENSORID = '1' and CHECKINOUT.CHECKTIME >= ('04/01/2014 08:15') and CHECKINOUT.CHECKTIME <= ('04/01/2014 09:00') ORDER BY CHECKTIME DESC ", con);
    sda.Fill(dt);
    dataGridView1.DataSource = dt;
}

正如您从上面的代码中看到的那样,我还没有实现GetDate()函数,因为每次我都收到错误,我希望我的程序尽可能顺利地运行。

任何得到高度赞赏和投票的帮助,我目前在C#环境中有一年的编程经验,因此我了解如何解决许多问题,但在使用SQL Server时无法实现正确的代码。

谢谢大家......

5 个答案:

答案 0 :(得分:0)

应该是:

CHECKINOUT.CHECKTIME >= DATEADD(day,DATEDIFF(day,'20140401',GETDATE(),
                                                 '2014-04-01T08:15:00') and
CHECKINOUT.CHECKTIME < DATEADD(day,DATEDIFF(day,'20140401',GETDATE(),
                                                '2014-04-01T09:00:00')

我也转而使用<作为结束点,因为这通常实际上是想要的(你通常希望在 9am之前严格发生的事件,并排除 at 上午9点发生的事件

您不会更改日期文字。唯一重要的是他们之间的关系。因此,我们首先计算出“2014年4月1日到当前日期时间之间已经过了多少天”,然后我们将相同的天数添加到2014年4月1日的08:15 - 最终结果是我们今天计算出的日期是08:15。

答案 1 :(得分:0)

SELECT ...
FROM ...
     CROSS APPLY (
         SELECT DATEADD(day, 0, DATEDIFF(day, 0, GETDATE())) AS Today
     ) AS CA1
     CROSS APPLY (
         SELECT DATEADD(minute, 8*60+15, Today) AS StartTime
               ,DATEADD(minute, 9*60, Today) AS EndTime
     ) AS CA2
WHERE CHECKINOUT.CHECKTIME BETWEEN StartTime AND EndTime

答案 2 :(得分:0)

为什么不将所需的DateTime值作为C#代码中的参数传递?

将代码更改为:

DateTime startTime = ...;
DateTime endTime = ...;

using (SqlCommand cmd = new SqlCommand("SELECT ... WHERE .. AND CHECKINOUT.CHECKTIME BETWEEN @startTime and @endTime ORDER BY CHECKTIME DESC ", con))
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
    cmd.Parameters.AddWithValue("@startTime", startTime);
    cmd.Parameters.AddWithValue("@endTime", endTime);
    sda.Fill(dt);
    dataGridView1.DataSource = dt;
}

将C#代码中的startTimeendTime设置为您实际想要查询的日期/时间。例如:

startTime = new DateTime(2014, 04, 02, 08, 15, 0);
endTime = new DateTime(2014, 04, 02, 09, 0, 0);

startTime = DateTime.Now.Date.AddHours(8).AddMinutes(15);
endTime = startTime.AddMinutes(45);

根据要求,这就是您的代码的外观:

private void button10_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = @"..."; 

    //Query all entries between 08:15 and 09:00 today
    DateTime startTime = DateTime.Now.Date.AddHours(8).AddMinutes(15);
    DateTime endTime = startTime.AddMinutes(45);

    // I use "using" to make sure command and data adapter are disposed of automatically
    // I use a separate command so I can add the parameters more easily.
    using (SqlCommand cmd = new SqlCommand("SELECT ... WHERE .. AND CHECKINOUT.CHECKTIME BETWEEN @startTime and @endTime ORDER BY CHECKTIME DESC ", con))
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
        // Pass in the two parameters to the query (note: I don't have to take care
        // of any formatting here - aren't parameterized queries a treat? :-)
        cmd.Parameters.AddWithValue("@startTime", startTime);
        cmd.Parameters.AddWithValue("@endTime", endTime);

        // Keep doing what you did before
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }
}

答案 3 :(得分:0)

如果是SQL SERVER 2008或以上

DECLARE @CurrentDate DATETIME  
SELECT @CurrentDate = CONVERT(DATE, GETDATE())

SELECT 
    USERINFO.USERID [User ID], 
    USERINFO.BADGENUMBER [Employee No.], 
    USERINFO.NAME [Name], 
    CHECKINOUT.CHECKTIME [Clock Time], 
    CHECKINOUT.CHECKTYPE [I/O], 
    CHECKINOUT.SENSORID [Sensor ID] 
FROM 
    USERINFO, 
    CHECKINOUT 
WHERE 
    USERINFO.USERID = CHECKINOUT.USERID 
    AND CHECKINOUT.CHECKTYPE = 'I' 
    AND CHECKINOUT.SENSORID = '1' 
    AND (CHECKINOUT.CHECKTIME BETWEEN (DATEADD (MINUTE, 495, @CurrentDate) AND DATEADD (MINUTE, 540, @CurrentDate))) 
ORDER BY CHECKTIME DESC

- 或 -

SELECT USERINFO.USERID [User ID], USERINFO.BADGENUMBER [Employee No.], USERINFO.NAME [Name], CHECKINOUT.CHECKTIME [Clock Time], CHECKINOUT.CHECKTYPE [I/O], CHECKINOUT.SENSORID [Sensor ID] FROM USERINFO, CHECKINOUT WHERE USERINFO.USERID = CHECKINOUT.USERID AND CHECKINOUT.CHECKTYPE = 'I' AND CHECKINOUT.SENSORID = '1' AND (CHECKINOUT.CHECKTIME BETWEEN DATEADD (MINUTE, 495, CONVERT(DATETIME, CONVERT(DATE, GETDATE()))) AND DATEADD (MINUTE, 540, CONVERT(DATETIME, CONVERT(DATE, GETDATE()))))) ORDER BY CHECKTIME DESC

答案 4 :(得分:-2)

这对你有用。一切顺利

SELECT USERINFO.USERID [User ID], USERINFO.BADGENUMBER [Employee No.], USERINFO.NAME [Name], CHECKINOUT.CHECKTIME [Clock Time], CHECKINOUT.CHECKTYPE [I/O], CHECKINOUT.SENSORID [Sensor ID] 
FROM USERINFO, CHECKINOUT 
WHERE USERINFO.USERID = CHECKINOUT.USERID 
    and CHECKINOUT.CHECKTYPE = 'I' 
    and CHECKINOUT.SENSORID = '1' 
    and CONVERT(varchar,CHECKINOUT.CHECKTIME,101) = CONVERT(varchar,GETDATE(),101)
    and CONVERT(varchar, CHECKINOUT.CHECKTIME,108) BETWEEN '08:15:00' AND '09:00:00'
ORDER BY CHECKTIME DESC