读书人

经过SQL来实现日期明细

发布时间: 2013-07-11 15:38:46 作者: rapoo

通过SQL来实现日期明细
我有一个表,存着员工和请假时间,数据表格式如下
name holiday_begin_date holiday_end_date
A 2013-01-16 2013-02-16
b 2013-02-05 2013-02-05
c 2013-02-15 2013-02-17
.. ............ ...........(以下数据省略)

假设,现在是2月份
现在想通过SQL来判断,2月的1~28号,所有员工的休假明细
即想通过当月的日期明细来left join 休假明细.

目前休假明细实现不了,请指教。谢谢

[解决办法]


Create function [dbo].[generateTime]

(

@begin_date datetime,

@end_date datetime

)

returns @t table(date datetime)

as

begin

with maco as

(

select @begin_date AS date

union all

select maco.date+1 as date from maco

where date+1 <=@end_date

)

insert into @t

select * from maco option(maxrecursion 0);

return

end

go
select * from dbo.generateTime('2013-2-1','2013-2-28')

读书人网 >SQL Server

热点推荐