我怎样才能找到特定的日子

发布于 2024-12-24 01:28:37 字数 293 浏览 1 评论 0原文

我有这样的日期值 - 12/2011 或 11/2011 (MM/yyyy)

我想找到特定月份的星期日......

例如,如果我选择月份 01 /2012,查询应该给出这样的结果

01
08
15
22
29

上面的日期是星期日。

01/2012 月的预期输出

01
08
15
22
29

如何进行查询

需要查询帮助

I have the date value like this - 12/2011 or 11/2011 (MM/yyyy)

I want to find the sundays on the particular month....

For Example, If i select the month 01/2012, The query should give the result like this

01
08
15
22
29

The above date are sunday.

Expected Output for 01/2012 Month

01
08
15
22
29

How to make a query

Need Query Help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

债姬 2024-12-31 01:28:37

借助数字表(master..spt_values

declare @M varchar(7)
set @M = '01/2012'

declare @D datetime
set @D = convert(datetime, '01/'+@M, 103)

set datefirst 7

select dateadd(day, N.Number, @D)
from master..spt_values as N
where N.type = 'P' and
      dateadd(day, N.Number, @D) >= @D and
      dateadd(day, N.Number, @D) < dateadd(month, 1, @D) and
      datepart(weekday, dateadd(day, N.Number, @D)) = 1

With a little help of a number table (master..spt_values)

declare @M varchar(7)
set @M = '01/2012'

declare @D datetime
set @D = convert(datetime, '01/'+@M, 103)

set datefirst 7

select dateadd(day, N.Number, @D)
from master..spt_values as N
where N.type = 'P' and
      dateadd(day, N.Number, @D) >= @D and
      dateadd(day, N.Number, @D) < dateadd(month, 1, @D) and
      datepart(weekday, dateadd(day, N.Number, @D)) = 1
脸赞 2024-12-31 01:28:37

来了:

SET DATEFIRST 1
DECLARE @givenMonth CHAR(7)
SET @givenMonth = '12/2011'

DECLARE @month INT 
SET @month = CAST(SUBSTRING(@givenMonth, 1, 2) AS INT)
DECLARE @year INT 
SET @year = CAST(SUBSTRING(@givenMonth, 4, 4) AS INT)

DECLARE @Date DATETIME 
SET @Date = DATEADD(month, @month - 1, CAST(CAST(@year AS CHAR(4)) AS DATETIME))
DECLARE @nextMonth DATETIME 
SET @nextMonth = DATEADD(MONTH, 1, @date)

DECLARE @firstSunday DATETIME 
SET @firstSunday = DATEADD(day, 7 - DATEPART(weekday, @date), @date)
CREATE TABLE #Days(Sunday INT)

WHILE @firstSunday < @nextMonth
BEGIN
    INSERT #Days
    SELECT DATEPART(DAY, @firstSunday) Sunday

    SET @firstSunday = DATEADD(day, 7, @firstSunday) 
END
SELECT Sunday
FROM #Days
ORDER BY 1

DROP TABLE #Days

Here it comes:

SET DATEFIRST 1
DECLARE @givenMonth CHAR(7)
SET @givenMonth = '12/2011'

DECLARE @month INT 
SET @month = CAST(SUBSTRING(@givenMonth, 1, 2) AS INT)
DECLARE @year INT 
SET @year = CAST(SUBSTRING(@givenMonth, 4, 4) AS INT)

DECLARE @Date DATETIME 
SET @Date = DATEADD(month, @month - 1, CAST(CAST(@year AS CHAR(4)) AS DATETIME))
DECLARE @nextMonth DATETIME 
SET @nextMonth = DATEADD(MONTH, 1, @date)

DECLARE @firstSunday DATETIME 
SET @firstSunday = DATEADD(day, 7 - DATEPART(weekday, @date), @date)
CREATE TABLE #Days(Sunday INT)

WHILE @firstSunday < @nextMonth
BEGIN
    INSERT #Days
    SELECT DATEPART(DAY, @firstSunday) Sunday

    SET @firstSunday = DATEADD(day, 7, @firstSunday) 
END
SELECT Sunday
FROM #Days
ORDER BY 1

DROP TABLE #Days
桃扇骨 2024-12-31 01:28:37

编辑:使用数字表代替 CTE,因为您使用的是 SQL Server 2000。来吧,升级并帮自己一个忙

DECLARE @monthyear varchar(10) = '11/2012';
DECLARE @start smalldatetime, @end smalldatetime;

-- use yyyymmdd format
SET @start = CAST(RIGHT(@monthyear, 4)+ LEFT(@monthyear, 2) + '01' AS smalldatetime);
-- work backwards from start of next month
SET @end = DATEADD(day, -1, DATEADD(month, 1, @start));

-- recursive CTE. Would be easier with a numbers table
;WITH daycte AS
(
    SELECT @start AS TheDay
    UNION ALL
    SELECT DATEADD(day, 1, TheDay) 
    FROM daycte
    WHERE TheDay < @end
)
SELECT DATENAME(day, TheDay)
FROM daycte 
-- One of many ways. 
-- This is independent of @@datefirst but fails with Chinese and Japanese language settings
WHERE DATENAME(weekday, TheDay) = 'Sunday';

Edit: use a numbers table in place of the CTE because you are in SQL Server 2000. C'mon, upgrade and do yourself a favour

DECLARE @monthyear varchar(10) = '11/2012';
DECLARE @start smalldatetime, @end smalldatetime;

-- use yyyymmdd format
SET @start = CAST(RIGHT(@monthyear, 4)+ LEFT(@monthyear, 2) + '01' AS smalldatetime);
-- work backwards from start of next month
SET @end = DATEADD(day, -1, DATEADD(month, 1, @start));

-- recursive CTE. Would be easier with a numbers table
;WITH daycte AS
(
    SELECT @start AS TheDay
    UNION ALL
    SELECT DATEADD(day, 1, TheDay) 
    FROM daycte
    WHERE TheDay < @end
)
SELECT DATENAME(day, TheDay)
FROM daycte 
-- One of many ways. 
-- This is independent of @@datefirst but fails with Chinese and Japanese language settings
WHERE DATENAME(weekday, TheDay) = 'Sunday';
秋叶绚丽 2024-12-31 01:28:37

您可以更改日期格式并使用 DateName 函数。

SELECT 日期名称(dw, GETDATE())

You can change date format and use DateName function.

SELECT DateName(dw, GETDATE())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文