sql自定义日期函数,返回范围内日期和星期数表。

Create function [dbo].[FUN_GenerateTime]
(
    @begin_date datetime, -- 起始时间
    @end_date datetime -- 结束时间
)
returns @t table(Weekday CHAR(6),Monthday datetime)
as
begin
    with maco as
    (
       select datename(dw,@begin_date) AS Weekday,@begin_date AS Monthday
       union all
       select datename(dw,Monthday+1),Monthday+1 from maco
       where Monthday+1 <=@end_date
    )
    insert into @t
    select * from maco option(maxrecursion 0);
    return
end
GO

调用方法:

SELECT * FROM FUN_GenerateTime('2017-01-01','2017-01-10')

返回结果:

原文地址:https://www.cnblogs.com/lbhqq/p/6393909.html