简体   繁体   中英

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.

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?

    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. 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') ---  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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