http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2464|回复: 0

[Sql Server] 计算任意两个时间之间的星期几的次数(纵向显示)

[复制链接]
发表于 2013-5-15 11:38:35 | 显示全部楼层 |阅读模式

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_weekdaycount]') and xtype in (N'FN', N'IF', N'TF'))
  2. drop function [dbo].[f_weekdaycount]
  3. GO
  4. /*--计算任意两个时间之间的星期几的次数(纵向显示)
  5. 本方法直接判断 @@datefirst 做对应处理
  6. 不受 sp_language 及 set datefirst 的影响  
  7. -
  8. /*--调用示例

  9. select * from f_weekdaycount('2004-8-02','2004-8-8')
  10. --*/
  11. create function f_weekdaycount(
  12. @dt_begin datetime,
  13. @dt_end datetime
  14. )returns table
  15. as
  16. return(
  17. select 项目='跨周数'
  18.   ,值=case when @dt_begin<@dt_end
  19.    then (datediff(day,@dt_begin,@dt_end)+7)/7
  20.    else (datediff(day,@dt_end,@dt_begin)+7)/7 end
  21. union all
  22. select a.a,case b.a
  23.   when -1 then case when a.b between b.b and b.c then 1 else 0 end
  24.   when  0 then case when b.b<=a.b then 1 else 0 end
  25.    +case when b.c>=a.b then 1 else 0 end
  26.   else b.a+case when b.b<=a.b then 1 else 0 end
  27.    +case when b.c>=a.b then 1 else 0 end
  28.   end
  29. from(select a='星期一',b=1
  30.   union all select '星期二',2 union all select '星期三',3
  31.   union all select '星期四',4 union all select '星期五',5
  32.   union all select '星期六',6 union all select '星期日',0
  33. )a,(select a=case when @dt_begin<@dt_end
  34.    then datediff(week,@dt_begin,@dt_end)-1
  35.    else datediff(week,@dt_end,@dt_begin)-1 end
  36.   ,b=case when @dt_begin<@dt_end
  37.    then (@@datefirst+datepart(weekday,@dt_begin)-1)%7
  38.    else (@@datefirst+datepart(weekday,@dt_end)-1)%7 end
  39.   ,c=case when @dt_begin<@dt_end
  40.    then (@@datefirst+datepart(weekday,@dt_end)-1)%7
  41.    else (@@datefirst+datepart(weekday,@dt_begin)-1)%7 end)b
  42. )
  43. go
复制代码


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-29 06:28

© 2014-2021

快速回复 返回顶部 返回列表