开发者

Find dates for a particular day for a given year and month + Sql Server 2005

开发者_JAVA百科

Given a month and year, how do I know the dates for a day in that month.

e.g. Given month = Feb, year = 2010. I need to find the sunday dates in the month

The expected output is:

07/02/2010
14/02/2010
21/02/2010
28/02/2010

How to do so?


Have a look at something like

DECLARE @Month VARCHAR(3),
        @Year INT

SELECT  @Month = 'Feb',
        @Year = 2010

DECLARE @RunDate DATETIME

SELECT @RunDate = '01 ' + @Month + CAST(@Year AS VARCHAR(4))

;WITH DATES AS(
        SELECT  @RunDate DateVal,
                DATENAME(dw, @RunDate) DateNameVal
        UNION ALL
        SELECT  DateVal + 1,
                DATENAME(dw, DateVal + 1)
        FROM Dates
        WHERE DATEPART(month,DateVal) = DATEPART(month,DateVal + 1)
)
SELECT  *
FROM    DATES
WHERE   DateNameVal = 'Sunday'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜