在两个给定日期之间搜索日期

时间:2015-04-23 11:50:02

标签: sql-server

我有两个具体日期。 让我们说从2014-04-04到2015-10-04。 我有一个计划,说Blah课程将在周一,周三和周五。 现在我想知道每个星期一,星期日和星期五的日期,从2014-04-04到2015-10-04。

5 个答案:

答案 0 :(得分:4)

使用日历表很简单:

SELECT t.* 
FROM dbo.TableName t
INNER JOIN CalendarTable c 
    ON t.DateColumn = c.Date
WHERE c.Date between '2014-04-04' AND '2015-10-04'
AND DATEPART(dw, c.Date) IN (1,3,5)

How to generate a calendar table(寻找“日历表”)。

答案 1 :(得分:2)

任何经验丰富的SQL Server老手的标准回复都是创建一个日历表。但这常常被嘲笑。所以这是一个和开箱即用的方法:

DECLARE @startDate DATE = '2014-04-04', @endDate DATE = '2015-10-04';

WITH CTE(N) AS (SELECT 1 FROM (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))a(N)),
CTE2(N) AS (SELECT 1 FROM CTE x CROSS JOIN CTE y),
CTE3(N) AS (SELECT 1 FROM CTE2 x CROSS JOIN CTE2 y),
CTE4(N) AS (SELECT 1 FROM CTE3 x CROSS JOIN CTE3 y),
CTE5(N) AS (SELECT 1 FROM CTE4 x CROSS JOIN CTE4 y),
CTE6(N) AS (SELECT 0 UNION ALL
            SELECT TOP (DATEDIFF(day,@startDate,@endDate)) 
            ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
            FROM CTE5),
TALLY(N) AS (SELECT DATEADD(day, N, @startDate)
             FROM CTE6
             WHERE DATENAME(weekday,DATEADD(day, N, @startDate)) IN ('Monday','Tuesday','Wednesday'))
SELECT N
FROM TALLY
ORDER BY N;

答案 2 :(得分:1)

如果您没有数字表,可以使用递归CTE。像这样的东西。请注意,DATEPART(weekday,Dates) IN(1,3,5)取决于您SELECT @@DATEFIRST的设置。

例如,如果@@DATEFIRST1,则使用DATEPART(weekday,Dates) IN(1,3,5)

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2014-04-04'
SET @EndDate = '2015-10-04'
;WITH CTE AS
(
    SELECT @StartDate Dates
    UNION ALL SELECT DATEADD(d,1,Dates) FROM CTE WHERE DATEADD(d,1,Dates) <=@EndDate
)

SELECT Dates,DATENAME(weekday,Dates) FROM CTE
WHERE DATEPART(weekday,Dates) IN(1,3,5)
OPTION (MAXRECURSION 0);
GO

答案 3 :(得分:0)

DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME SET @StartDate ='2014-04-04' SET @EndDate ='2015-10-04'

;与CTE AS (     SELECT @StartDate日期     UNION ALL SELECT DATEADD(d,1,Dates)FROM CTE WHERE DATEADD(d,1,Dates)&lt; = @ EndDate )

选择日期,DATENAME(工作日,日期)来自CTE 在哪里DATEPART(工作日,日期)IN(1,3,5) 选项(MAXRECURSION 0); GO

---感谢@ughai

答案 4 :(得分:0)

我想出了另一种方法来做到这一点。 此查询不仅会显示两者之间的日期,还会比较您想要与之比较的日期。

Q.Dates比较

DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME SET @StartDate ='2014-12-22' SET @EndDate ='2015-02-13'

drop table #TabCourseDetailID  SELECT ROW_NUMBER()OVER(ORD BY CourseDetailID asc)AS Row,              CourseDetailID  进入#TabCourseDetailID
 来自coursedetail  其中UnitType ='T'且courseID = 1

将@counter声明为int  声明@CourseDetailID varchar(500)  设置@counter = 0

声明@TempTable表(FirstValue datetime,LastValue int)

而@StartDate&lt; = @EndDate     开始          如果DATEPART(WeekDay,@ StartDate)IN(2,4,6)              开始

            Set @Counter = @counter+1 

            Select @CoursedetailId=CoursedetailId 
                from #TabCourseDetailID 
            where row=@counter

            insert into @TempTable (FirstValue,LastValue) 
            values                 (@StartDate,@CoursedetailId)

         end 
set @StartDate =DATEADD(d,1,@StartDate)
end 




select t.LastValue,
        t.FirstValue,
        bp.conductedDate  
from batchprogress bp
Join @TempTable t on (t.LastValue=bp.CoursedetailID)
where CourseID =1 and batchID =5
order by  t.lastvalue