if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[f_weekdaycount]') and xtype in (n'fn', n'if', n'tf'))
drop function [dbo].[f_weekdaycount]
go/*--計算任意兩個時間之間的星期幾的次數(縱向顯示)
本方法直接判斷 @@datefirst 做對應處理
不受 sp_language 及 set datefirst 的影響
--鄒建 2004.08(引用請保留此資訊)--*/
/*--呼叫示例
select * from f_weekdaycount('2004-8-02','2004-8-8')
--*/
create function f_weekdaycount(
@dt_begin datetime,
@dt_end datetime
)returns table
asreturn(
select 專案='跨週數'
,值=case when @dt_begin<@dt_end
then (datediff(day,@dt_begin,@dt_end)+7)/7
else (datediff(day,@dt_end,@dt_begin)+7)/7 end
union all
select a.a,case b.a
when -1 then case when a.b between b.b and b.c then 1 else 0 end
when 0 then case when b.b<=a.b then 1 else 0 end
+case when b.c>=a.b then 1 else 0 end
else b.a+case when b.b<=a.b then 1 else 0 end
+case when b.c>=a.b then 1 else 0 end
endfrom(select a='星期一',b=1
union all select '星期二',2 union all select '星期三',3
union all select '星期四',4 union all select '星期五',5
union all select '星期六',6 union all select '星期日',0
)a,(select a=case when @dt_begin<@dt_end
then datediff(week,@dt_begin,@dt_end)-1
else datediff(week,@dt_end,@dt_begin)-1 end
,b=case when @dt_begin<@dt_end
then (@@datefirst+datepart(weekday,@dt_begin)-1)%7
else (@@datefirst+datepart(weekday,@dt_end)-1)%7 end
,c=case when @dt_begin<@dt_end
then (@@datefirst+datepart(weekday,@dt_end)-1)%7
else (@@datefirst+datepart(weekday,@dt_begin)-1)%7 end)b )
go
任意兩個時間之間的星期幾的次數 縱 sql
if exists select from dbo.sysobjects where id object id n dbo f weekdaycount and xtype in n fn n if n tf drop function dbo f weekdaycount go 計算任意兩個時間之...
獲取當天是星期幾,計算兩個日期相隔時間
function tformmain.getweek string var date systemtime begin getlocaltime date case date.wdayofweek of 0 result 星期天 1 result 星期一 2 result 星期二 3 result ...
php計算任意兩個日期之間的天數
php計算任意兩個日期之間的天數 基本思想,算術減法 cur date 2009 04 11 last date 2008 12 20 date dist 0000 03 21 間隔大約3個月21天 116天 create function function date distance last d...