SQL Server从_date和End_date开始的日期

时间:2021-02-14 10:20:27

I have event table where I need to select records between days. my statements look like

我有事件表,我需要在几天之间选择记录。我的陈述看起来像

Select 
    Event_ID
    ,Event_Type
    ,Event_Time
From 
    Event_Table
Where 
    CONVERT(char(8),Event_Table.Event_time,112) BETWEEN '20110501' AND '20110505'

The Event_time is DateTime format.

Event_time是DateTime格式。

Now every thing look fine exsipt what I should do if I need the statement to request the date betwon as Date_start and Date_End any time it run. The idea is to request a new between date any time the scrpt run

现在,如果我需要声明在任何时候运行Date_start和Date_End之前请求日期,那么每件事看起来都很好。我们的想法是在scrpt运行的任何时候请求新的日期

1 个解决方案

#1


3  

declare @EndDate datetime 
set @EndDate = getdate()

declare @StartDate datetime
set @StartDate= dateadd(day, -5, @EndDate)

select Event_ID, Event_Type, Event_Time
from Event_Table
where 
    Event_Time >= @StartDate and
    Event_Time < @EndDate

if you need to include @EndDate then

如果你需要包含@EndDate那么

select Event_ID, Event_Type, Event_Time
from Event_Table
where 
    Event_Time >= @StartDate and
    Event_Time < dateadd(day, 1, @EndDate)

#1


3  

declare @EndDate datetime 
set @EndDate = getdate()

declare @StartDate datetime
set @StartDate= dateadd(day, -5, @EndDate)

select Event_ID, Event_Type, Event_Time
from Event_Table
where 
    Event_Time >= @StartDate and
    Event_Time < @EndDate

if you need to include @EndDate then

如果你需要包含@EndDate那么

select Event_ID, Event_Type, Event_Time
from Event_Table
where 
    Event_Time >= @StartDate and
    Event_Time < dateadd(day, 1, @EndDate)