[原] Sql Server 获取某年某月有多少个工作日(仅不包含星期日)

--獲取某年某月有多少周日(不包含周六)
CREATE FUNCTION f_getnums ( @year_month VARCHAR(8) )
RETURNS INT
AS 
    BEGIN
        DECLARE @bdt DATETIME ,
            @edt DATETIME ,
            @i INT
        SET @i = 0
        SET @bdt = CAST(@year_month + '-01' AS DATETIME)
        SET @edt = DATEADD(d, -1,
                           DATEADD(month, 1,
                                   CAST(@year_month + '-01' AS DATETIME)))

        WHILE DATEDIFF(d, @bdt, @edt) >= 0 
            BEGIN
                IF --DATEPART(dw, @bdt) = 1                    OR
                    DATEPART(dw, @bdt) = 7 
                    BEGIN
                        SET @i = @i + 1
                    END
                SET @bdt = DATEADD(d, 1, @bdt)
            END
        RETURN @i
    END

DECLARE @date DATETIME 
SET @date = '2012-12-01'
 --1.獲取某年某月有多少天
 SELECT  DATEDIFF(dd, @date, DATEADD(mm, 1, @date))  
 --2.获取某年某月有多少个星期日
 select dbo.f_getnums(CONVERT(VARCHAR(07), @date, 120)) AS workday 
 --3.获取某年某月有多少个工作日(仅不包含周日)
 select DATEDIFF(dd, @date, DATEADD(mm, 1, @date))- dbo.f_getnums(CONVERT(VARCHAR(07), @date, 120)) AS workday 

 

你可能感兴趣的:([原] Sql Server 获取某年某月有多少个工作日(仅不包含星期日))