在 sql 中获取当前月份的详细信息。比如,一个月有多少周?有名字的工作日数?

Get current month details in sql. Like, how many weeks in month?, Count of weekdays with there name?

今天突然想到一个想法,做一个动态查询,将当月的所有详细信息合并为一个 table,假设如果当前月份是 2017 年 5 月,那么输出 table 应该是像这样:

Details          |   Count
-----------------|------------
First Date       |  05-01-2017
Last Date        |  05-31-2017
Count of Mon     |  5
Count of Tues    |  5
Count of Wed     |  5
Count of Thur    |  4
Count of Fri     |  4
Count of Sat     |  4
Count of Sun     |  4

通过合并下一个链接:-

How can I select the first day of a month in SQL?

SQL Query to find the last day of the month

Count how many Sundays, Mondays, Tuesdays... in current month using MS SQL

尝试下一个代码:-

declare @T table(Details varchar(100) , Result varchar(100));

WITH CTE AS
(
    SELECT DATEADD(D,-DATEPART(D,GETDATE())+1,GETDATE())[FIRST SUNDAY DATE],DATENAME(DW,DATEADD(D,-DATEPART(D,GETDATE())+1,GETDATE()))[DAY NAME]
    UNION ALL
    SELECT DATEADD(D,1,[FIRST SUNDAY DATE]),DATENAME(DW,DATEADD(D,1,[FIRST SUNDAY DATE]))FROM CTE WHERE [FIRST SUNDAY DATE]<=DATEADD(D,-DATEPART(D,GETDATE()),DATEADD(M,1,GETDATE()))-1
)
insert into @T(Details , Result)
SELECT [DAY NAME],COUNT([DAY NAME]) as 'COUNT' FROM CTE GROUP BY [DAY NAME]

Update @T
set Details = 'Count OF ' + Details

SELECT 'First Date' 'Details', convert(varchar(100),DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0),101) 'Count'
Union all
select 'Last Date' , convert(varchar(100),dateadd(month,1+datediff(month,0,GETDATE()),-1),101)
union all
select * from @T

结果:-

IF OBJECT_ID('Tempdb..#MonthDetails') IS NOT NULL
Drop Table #MonthDetails

Declare @monthdate DATE ='05-5-2017'--Provide date
SET @monthdate =CONVERT(VARCHAR(10), @monthdate, 105)

DECLARE @y INT = DATEPART(YEAR,@monthdate),
        @EndOFMonth Varchar(10),
        @StartOfMonth Varchar(10)

CREATE TABLE #MonthDetails(ID Int IDENTITY,[MonthName] Varchar(10),EndOFMonth Varchar(10),StartOfMonth Varchar(10)
,[Mon] INT,[Tue] INT,[Wed] INT,[Thu] INT, [Fri] INT,[Sat] INT,[Sun] INT,[Sum] INT)


SET @EndOFMonth= CONVERT(VARCHAR(10),EOMONTH(@monthdate),105) 
SET @StartOfMonth=CONVERT(VARCHAR(10),DATEADD(month, DATEDIFF(month, 0, @monthdate), 0),105) 

DECLARE @d DATETIME = dateadd(year, @y - 1900, 0)
;WITH CTE
AS
(
    SELECT 1 A, 
      LEFT(DATENAME(WEEKDAY, @D), 3) B,
      DATENAME(MONTH, 0) MONTH,
      1 SORT
    UNION ALL
    SELECT A + 1 A, 
      LEFT(DATENAME(WEEKDAY, @D + A), 3) B, 
      DATENAME(MONTH, @D + A) MONTH, 
      DATEPART(MONTH, @D + A) SORT
    FROM CTE WHERE A < DATEPART(DAYOFYEAR, DATEADD(YEAR, 1, @D)-1)
)
INSERT INTO #MonthDetails([MonthName],EndOFMonth,StartOfMonth,[Mon],[Tue],[Wed],[Thu],[Fri],[Sat],[Sun],[Sum])
SELECT  [MONTH],
      @EndOFMonth,
     @StartOfMonth,

     [Mon],[Tue],[Wed],[Thu],[Fri],[Sat],[Sun], 
     [Mon]+[Tue]+[Wed]+[Thu]+[Fri]+[Sat]+[Sun] [Sum] 
FROM cte
PIVOT (COUNT(a) FOR [b] IN ([Mon],[Tue],[Wed],[Thu],[Fri],[Sat],[Sun],[Sum])) AS pvt WHERE [MONTH]=DATENAME(MM,@monthdate)
ORDER BY sort 
OPTION (MAXRECURSION 366)

;With Result
AS
(

SELECT ISNULL(NULL,'FirstDate') AS Details ,StartOfMonth AS [Count]                 FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'LastDate') ,EndOFMonth                                          FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Mon')  , CAST([Mon] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Tues') , CAST([Tue] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Wed')  , CAST([Wed] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Thur') , CAST([Thu] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Fri')  , CAST([Fri] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Sat')  , CAST([Sat] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL
SELECT ISNULL(NULL,'Count of Sun')  , CAST([Sun] AS VARCHAR(5))                     FROM #MonthDetails  UNION ALL                 
SELECT ISNULL(NULL,'Total Days Of Month'),CAST([Sum]  AS VARCHAR(5))                FROM #MonthDetails
)
SELECT * from  Result

输出

Details                 |Count
-------------------------------------       
FirstDate               |01-01-2017
LastDate                |31-01-2017
Count of Mon            |5
Count of Tues           |5
Count of Wed            |4
Count of Thur           |4
Count of Fri            |4
Count of Sat            |4
Count of Sun            |5
Total Days Of Month     |31