博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
任意两个时间之间的星期几的次数-纵.sql
阅读量:7033 次
发布时间:2019-06-28

本文共 1424 字,大约阅读时间需要 4 分钟。

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
as
return(
    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
        end
    from(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

转载地址:http://thual.baihongyu.com/

你可能感兴趣的文章
MacOS下安装MongoDB数据库
查看>>
Git常用命令
查看>>
libevent学习
查看>>
动态代理的几种方式
查看>>
Collections常用方法总结
查看>>
微信小程序
查看>>
bash变量
查看>>
知识点049-supervisor
查看>>
干货满满,Android热修复方案介绍
查看>>
罗振宇跨年演讲之夜 阿里云护航得到App
查看>>
django中间键
查看>>
2017/09/22脚本练习
查看>>
Http post 常用的四种请求方式
查看>>
Python常用模块2
查看>>
直播APP开发如何得到用户青睐?
查看>>
Day26 正则介绍_grep工具
查看>>
数据库-删除数据
查看>>
753个大奖邀请各位新老博主入驻云栖,请技术人员广而告之
查看>>
apache域名重定向rewrite
查看>>
LNMP架构介绍,php安装
查看>>