SQL语句确定一个月中的星期一数

时间:2011-08-10 09:58:57

标签: sql-server

有没有办法在不使用T-SQL的情况下获得给定月份(和年份)的星期一数量?

由于

3 个答案:

答案 0 :(得分:4)

我不明白你的意思是说:

  

有没有办法获得给定月份的星期一数量(和   年)不使用T-SQL?

如果您希望在所有数据库中执行此通用代码片段,请忘记它。我怀疑你甚至能够在两个不同的数据库上运行一个版本。日期和工作日之类的事情往往在数据库供应商之间以不同的方式实现。

这是TSQL方式(星期一月计数):

;with AllDates AS
(SELECT CONVERT(datetime,CONVERT(varchar(6),GETDATE(),112)+'01') AS DateOf
 UNION ALL
 SELECT DateOf+1
 FROM AllDates
 WHERE
 MONTH(DateOf+1)=MONTH(CONVERT(datetime,CONVERT(varchar(6),GETDATE(),112)+'01'))
)
SELECT COUNT(DateOf) AS MondayCountMonth
FROM AllDates 
WHERE DATENAME(weekday,DateOf)='Monday'

这是TSQL方式(星期一年份):

;with AllDates AS
(SELECT CONVERT(datetime,CONVERT(varchar(4),GETDATE(),112)+'0101') AS DateOf
 UNION ALL
 SELECT DateOf+1
 FROM AllDates
 WHERE
 YEAR(DateOf+1)=Year(CONVERT(datetime,CONVERT(varchar(4),GETDATE(),112)+'0101'))
)
SELECT COUNT(DateOf) AS MondayCountYear
FROM AllDates 
WHERE DATENAME(weekday,DateOf)='Monday'
OPTION (MAXRECURSION 367)

编辑基于OP评论,这是一个版本,它将每月和每年的星期一计数视为另一个查询中的子查询:

DECLARE @YourTable table (Col1 int, Col2 varchar(5))
INSERT @YourTable VALUES (1,'aaa')
INSERT @YourTable VALUES (2,'bbb')
INSERT @YourTable VALUES (3,'ccc')

;with MonthMondayCount AS
(SELECT CONVERT(datetime,CONVERT(varchar(6),GETDATE(),112)+'01') AS DateOf
 UNION ALL
 SELECT DateOf+1
 FROM MonthMondayCount
 WHERE
 MONTH(DateOf+1)=MONTH(CONVERT(datetime,CONVERT(varchar(6),GETDATE(),112)+'01'))
)
,YearMondayCount AS
(SELECT CONVERT(datetime,CONVERT(varchar(4),GETDATE(),112)+'0101') AS DateOf
 UNION ALL
 SELECT DateOf+1
 FROM YearMondayCount
 WHERE
 YEAR(DateOf+1)=Year(CONVERT(datetime,CONVERT(varchar(4),GETDATE(),112)+'0101'))
)
SELECT
    y.*
    ,(SELECT COUNT(DateOf) AS MondayCountMonth FROM MonthMondayCount WHERE DATENAME(weekday,DateOf)='Monday') AS MondayCountMonth
    ,(SELECT COUNT(DateOf) AS MondayCountYear FROM YearMondayCount WHERE DATENAME(weekday,DateOf)='Monday') AS MondayCountYear
    FROM @YourTable  y
    OPTION (MAXRECURSION 367)

输出:

Col1        Col2  MondayCountMonth MondayCountYear
----------- ----- ---------------- ---------------
1           aaa   5                52
2           bbb   5                52
3           ccc   5                52

(3 row(s) affected) 

答案 1 :(得分:1)

( DATEADD( DAY, -1, DATEADD(  MONTH, 1 , month + '-01' ) -
  DATEADD( DAY, 7 - DATEPART( WEEKDAY, month + '-01' ), month + '-01' )
) DIV 7 + 1

我不知道这是多少ANSI SQL兼容,但是它在MySql中工作(不是在更改之后,它现在应该在SQL Server中工作)。

month应采用'yyyy-mm'格式

答案 2 :(得分:1)

试试这个:)

DECLARE @tmpDate as date
set @tmpDate  = getdate(); --you can add any date
DECLARE @Startdate as varchar( 8)
DECLARE @Enddate as varchar( 8)
SELECT @Startdate = replace(convert(varchar,cast(DATEADD(month, DATEDIFF(month, 0, @tmpDate), 0) as date) , 111), '/', '');
SELECT @Enddate =  replace(convert(varchar, cast(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@tmpDate)+1,0)) as date), 111), '/', '');

with [dates] as (
    select  convert(date , @Startdate  ) as [date] --start
    union all
    select   dateadd(day , 1 , [date]) 
    from [dates]
    where [date] < @Enddate )
Select X.WeekDayNumber, count(X.WeekDayNumber) as NumberOfDays
from (
SELECT [date] , DATEPART(weekday,[date] ) as WeekDayNumber
from [dates]
WHERE [date] IS NOT NULL)X
Group  by X.WeekDayNumber
option (maxrecursion 0 )