生成日期列表(工作日,休息日,全部日期)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO

/*--生成日期列表
   
    生成指定年份的工作日/休息日列表

--邹建 2003.12(引用请保留此信息)--
*/

/*--调用示例

    --查询 2003 年的工作日列表
    SELECT * FROM dbo.f_getdate(2003,0)
   
    --查询 2003 年的休息日列表
    SELECT * FROM dbo.f_getdate(2003,1)

    --查询 2003 年全部日期列表
    SELECT * FROM dbo.f_getdate(2003,NULL)
--
*/
CREATE FUNCTION dbo.f_getdate(
@year int,    --要查询的年份
@bz bit       --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
   
DECLARE @tb TABLE(ID int IDENTITY(0,1),Date datetime)
   
INSERT INTO @tb(Date) SELECT TOP 366 DATEADD(Year,@YEAR-1900,'1900-1-1')
   
FROM sysobjects a ,sysobjects b
   
UPDATE @tb SET Date=DATEADD(DAY,id,Date)
   
DELETE FROM @tb WHERE Date>DATEADD(Year,@YEAR-1900,'1900-12-31')
   
   
IF @bz=0
       
INSERT INTO @re(Date,Weekday)
       
SELECT Date,DATENAME(Weekday,Date)
       
FROM @tb
       
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
   
ELSE IF @bz=1
       
INSERT INTO @re(Date,Weekday)
       
SELECT Date,DATENAME(Weekday,Date)
       
FROM @tb
       
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 IN (0,6)
   
ELSE
       
INSERT INTO @re(Date,Weekday)
       
SELECT Date,DATENAME(Weekday,Date)
       
FROM @tb
       
   
RETURN
END
GO


/*====================================================================*/

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO

/*--生成列表

    生成指定日期段的日期列表

--邹建 2005.03(引用请保留此信息)--
*/

/*--调用示例

    --查询工作日
    SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',0)
   
    --查询休息日
    SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',1)
   
    --查询全部日期
    SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',NULL)
--
*/

CREATE FUNCTION dbo.f_getdate(
@begin_date Datetime--要查询的开始日期
@end_date Datetime,    --要查询的结束日期
@bz bit                --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
   
DECLARE @tb TABLE(ID int IDENTITY(0,1),a bit)
   
INSERT INTO @tb(a) SELECT TOP 366 0
   
FROM sysobjects a ,sysobjects b
   
   
IF @bz=0
       
WHILE @begin_date<=@end_date
       
BEGIN
           
INSERT INTO @re(Date,Weekday)
           
SELECT Date,DATENAME(Weekday,Date)
           
FROM(
               
SELECT Date=DATEADD(Day,ID,@begin_date)
               
FROM @tb               
            )a
WHERE Date<=@end_date
               
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
           
SET @begin_date=DATEADD(Day,366,@begin_date)
       
END
   
ELSE IF @bz=1
       
WHILE @begin_date<=@end_date
       
BEGIN
           
INSERT INTO @re(Date,Weekday)
           
SELECT Date,DATENAME(Weekday,Date)
           
FROM(
               
SELECT Date=DATEADD(Day,ID,@begin_date)
               
FROM @tb               
            )a
WHERE Date<=@end_date
               
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 in(0,6)
           
SET @begin_date=DATEADD(Day,366,@begin_date)
       
END
   
ELSE
       
WHILE @begin_date<=@end_date
       
BEGIN
           
INSERT INTO @re(Date,Weekday)
           
SELECT Date,DATENAME(Weekday,Date)
           
FROM(
               
SELECT Date=DATEADD(Day,ID,@begin_date)
               
FROM @tb               
            )a
WHERE Date<=@end_date
           
SET @begin_date=DATEADD(Day,366,@begin_date)
       
END

   
RETURN
END
GO

 

---给一个日期,生成本周的日期列表:

declare @date datetime
set @date='20090423'
select dateadd(dd,a.number,@date+2-datepart(dw,@date))

from master..spt_values a

where type='p'

    and number<=6

 

declare @d datetime
set @d = getdate()

select dateadd(d,n,@d) as t
from (select -1 as n union select -2 union select -3 union select -4 union select -5 union select -6 union
     
select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 0) t
where datepart(wk,@d)=datepart(wk,dateadd(d,n,@d))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值