简体   繁体   English

设置开始和结束日期

[英]Setting Start and End dates

I have a simple query that needs to pull in all employees that were hired in the past 10 days, up until end of day yesterday at 11:59, excluding the current date, using Start & End date variables.我有一个简单的查询,需要使用开始和结束日期变量提取过去 10 天内雇用的所有员工,直到昨天 11:59 结束,不包括当前日期。

I know there's MANY ways of setting this date range.我知道有很多方法可以设置这个日期范围。 What is the most efficient SQL server expression that can give you the start and end dates for the past 10 days, excluding the current date?什么是最有效的 SQL 服务器表达式,可以为您提供过去 10 天的开始和结束日期,不包括当前日期?

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

    SELECT @StartDate, @EndDate

Desired outcome:期望的结果:

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

The:59 schemes are a bad idea. The:59 计划是个坏主意。 Comparisons should be less than the (start of) next day in order to include the full previous day.比较应该少于第二天的(开始),以便包括完整的前一天。

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

However if you still want it:但是,如果您仍然想要它:

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

Hope this Code Works fine for your Case:希望此代码适用于您的案例:

SELECT 

@Start=CAST(CAST(GETDATE()-10 AS DATE) AS DATETIME),
@End=CONCAT(CAST(GETDATE()-1 AS DATE),' 23:59:59.000') ---  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM