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

Sql 我怎样才能找到特定的日子,sql,sql-server,sql-server-2000,Sql,Sql Server,Sql Server 2000,我有这样的日期值-2011年12月或2011年11月MM/yyyy 我想找出某个月的星期日 例如,如果我选择2012年1月,则查询应给出如下结果 01 08 15 22 29 上述日期为星期日 2012年1月的预期产出 如何进行查询 需要查询帮助编辑:使用数字表代替CTE,因为您使用的是SQL Server 2000。来吧,升级,帮你自己一个忙 DECLARE @monthyear varchar(10) = '11/2012'; DECLARE @start smalldatetime, @

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

我想找出某个月的星期日

例如,如果我选择2012年1月,则查询应给出如下结果

01
08
15
22
29
上述日期为星期日

2012年1月的预期产出

如何进行查询


需要查询帮助

编辑:使用数字表代替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';

编辑:使用数字表代替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';
来了:

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
来了:

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

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


选择DateNamedw,GETDATE

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


选择DateNamedw,GETDATE

,借助数字表主控..spt\u值的一点帮助


在数字表主控程序的帮助下..spt_值


您真的将月份存储为mm/yyyy吗?为什么yyyymm更接近ISO和可排序?您真的将月份存储为mm/yyyy吗?为什么yyyymm更接近ISO和可排序?在SQL Server 2000中,我希望:-.@MichałPowaga:啊,错过了。你知道,现在是2012年,SQL Server 2000发布了13年,发布了几个版本…@gbn在英国文化中,它将工作8-,但如果周日是俄语或希伯来语,那么我们有一个问题8-,正如你所注意到的。在SQL Server 2000中,我希望:-。MichałPowaga:啊,错过了。你知道,现在是2012年,SQL Server 2000发布了13年,在英国文化中,@gbn可以工作8-,但如果周日是俄语或希伯来语,那么我们就有一个问题8-,正如你所注意到的那样。+1我一直在寻找类似master的东西。.sptרu值很多次,谢谢:-.+1我一直在寻找像大师一样的东西。spt_值很多次,谢谢:-。