if
exists
(select
*from
dbo.sysobjects
where
id =
object_id(n'
[tb_holiday]')
andobjectproperty
(id, n
'isusertable')
=1) drop
table
[tb_holiday]go
--定義節假日表
create
table
tb_holiday(
hdate
**alldatetime
primary
keyclustered
, --
節假日期
name
nvarchar(50
) not
null
) --
假日名稱
goif
exists
(select
*from
dbo.sysobjects
where
id =
object_id(n'
[dbo].[f_workday]')
andxtype
in(n'fn
', n'if
', n'tf
'))
drop
function
[dbo].
[f_workday]go
--計算兩個日期之間的工作天數
create
function
f_workday
( @dt_begin
datetime
, --
計算的開始日期
@dt_end
datetime
--計算的結束日期
)returns
intas
begin
if@dt_begin
>
@dt_end
return
(datediff
(day
,@dt_begin
,@dt_end) +
1-( select
count(*
) from
tb_holiday
where
hdate
between
@dt_begin
and@dt_end
)) return(-
(datediff
(day
,@dt_end
,@dt_begin) +
1-( select
count(*
) from
tb_holiday
where
hdate
between
@dt_end
and@dt_begin
)))
endgo
ifexists
(select
*from
dbo.sysobjects
where
id =
object_id(n'
[dbo].[f_workdayadd]')
andxtype
in(n'fn
', n'if
', n'tf
'))
drop
function
[dbo].
[f_workdayadd]go
--在指定日期上增加工作天數
create
function
f_workdayadd(
@date
datetime
, --
基礎日期
@workday
int--
要增加的工作日數
)returns
datetime
asbegin
if@workday
>
0while
@workday
>
0select
@date
=@date
+@workday
,@workday
=count(*
) from
tb_holiday
where
hdate
between
@date
and@date
+@workday
else
while
@workday
<
0select
@date
=@date
+@workday
,@workday
=-count(*
) from
tb_holiday
where
hdate
between
@date
and@date
+@workday
return
(@date
) end
SQL 求兩個日期值之間的工作天數
讀書 oracle查詢優化改寫 後有感。以emp表為例,我們需要求得hiredate的最大值與最小值之間的工作天數。首先,我們需要求出max和min的hiredate sql select max t.hiredate as maxdate,min t.hiredate as mindate fro...
計算兩個日期之間的工作日數
計算兩個日期之間的工作日數,星期6,星期天,不算工作日 dt1和dt2之間相隔多少工作日,其中dt3 dt4的時間為公休日,這裡公休日可以用以個陣列,或者從乙個xml表裡面讀取,以便扣除 要計算的起始時間 要計算的結束時間 公休起始時間 公休結束時間 intreturn private int di...
計算兩個日期之間的天數
問題描述 給定兩個日期,計算相差的天數。比如2010 1 1和2010 1 3相差2天。時間限制 1000 記憶體限制 65536 輸入共兩行 第一行包含三個整數startyear,startmonth,startday,分別是起始年 月 日。第二行包含三個整數endyear,endmonth,en...