设置开始和结束日期

时间:2020-04-12 20:27:29

标签: sql sql-server tsql date

我有一个简单的查询,需要使用“开始和结束日期”变量来提取过去10天(直到昨天的11:59,直到当前日期)为止的过去10天雇用的所有员工。

我知道有许多设置此日期范围的方法。 能为您提供过去10天(不包括当前日期)的开始和结束日期的最有效的SQL Server表达式是什么?

    DECLARE @StartDate = ???
    DECLARE @EndDate = ???

    SELECT @StartDate, @EndDate

所需结果:

    2020-04-02 00:00:00.000, 2020-04-11 23:59:59.000

2 个答案:

答案 0 :(得分:3)

:59方案不是一个好主意。比较应小于第二天(的开始),以便包括前一天的整个时间。

private static void AddToStockLoop(int itemid, int itemquantity, double itemprice) 
{

   if (itemid <= 0)
   {
      Console.WriteLine("Item ID Cannot Be Less Than 1, Please Try Again");
      itemid = ReadInteger("Item ID:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemid
   }

   if (itemquantity <= 0)
   {
      Console.WriteLine("Quantity Cannot Be Less Than 1, Please Try Again");
      itemquantity = ReadInteger("Quantity:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemquantity     
   }

   if (itemprice < 0.01)
   {
      Console.WriteLine("Item Price Cannot Be Less Than 0, Please Try Again");
      itemprice = ReadDouble("Item Price:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemprice
   }
}

但是,如果您仍然想要它:

SELECT @StartDate = DATEADD(dd,-10,CAST(getdate() AS DATE)), @EndDate = CAST(getdate() AS DATE)

答案 1 :(得分:0)

希望此代码对您的情况适用:

connect(mBulbWindow, &MBulbWindow::transferEntity, this, &MainWindow::addEntity)