问题之间的Sql日期

时间:2017-05-27 21:16:37

标签: sql-server date datetime

我正在编写存储过程。我想得到日期在每个星期二之间的表元素列表。我的意思是,例如,如果人员执行程序今天的日期是2017-05-28,我想获得2017-05-23 00:00:00.000和2017-05-30 00:00:00.000之间的列表。如果今天的日期是2017-05-23 00:00:00.000和2017-05-30 00:00:00.000之间的2017-05-28。如果日期是2017-04-01 00:00:00.000和2017-05-02 00:00:00.000之间的2017-05-01 。 我写了

GO
/****** Object:  StoredProcedure [dbo].[mobile_group_match_list]    Script Date: 05/28/2017 00:03:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================

-- =============================================
ALTER PROCEDURE [dbo].[mobile_group_match_list]

    @refGroup bigint
AS
BEGIN

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    SELECT refLeaugeTeam INTO #TEAMS FROM [GROUP_TEAM] gt WHERE gt.refGroup=@refGroup

    SELECT Id,[Guid],HomeTeam,AwayTeam,HomePoint,EqualPoint,AwayPoint,MatchDate,HomeScore,AwayScore,@refGroup as refGroup
    FROM MATCH m
    WHERE ( (m.HomeTeam IN (SELECT refLeaugeTeam FROM #TEAMS)) OR (m.AwayTeam IN (SELECT refLeaugeTeam FROM #TEAMS)) )
    AND MatchDate>=DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 1)
    AND MatchDate<DATEADD(ww, DATEDIFF(ww,0,GETDATE()) + 1, 1)

    SELECT lt.Id,T.Team FROM TEAM t,LEAUGE_TEAM lt WHERE lt.refTeam=t.Id
END

日期不起作用它给出2017-05-30 00:00:00.000和2017-06-06 00:00:00.000但是必须是2017-05-23 00:00:00.000并且30 00:00:00.000

我该如何解决这个问题?提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个。

AND MatchDate>=dateadd(dd,datediff(dd,0,getdate())/7 * 7 + 1,0)
    AND MatchDate<dateadd(dd,datediff(dd,-7,getdate())/7 * 7 + 1,0)