T-SQL 2 Tips: 1.计算任意两日期之间的"周一"到"周日"分别各有几个! 2.根据出生日期计算精确年龄!

原创 2004年08月20日 03:39:00

这两个小技巧,不写不知道,一写吓一跳!
都是看似简单,实际做起来就懵,得仔细想一想,才能写对!
凡是有日期运算的程序都要细心哦!

先说第二个:

2.根据出生日期精确计算年龄!
  所谓计算精确年龄就是: 生日差一天也不能长一岁!
  大家常用,间隔年数算作年龄! 如果需求要精确,如: 保险 之类的,就粗了!
  当然还可引申为根据入职日期计算精确的司龄,算加薪之类的需求!
  我起初认为很简单,当年也写了好几遍才写对!高手们也被我晃点了数次几近晕倒!
  不信有当年 2002-11-27 16:16:26 贴子为证:

  《我的表中有字段出生年月日,请问怎样根据当前日期计算该人的年龄,并填充到表的相应字段内?》
   http://search.csdn.net/expert/topicview1.asp?id=1210302

  书归正传:

declare @Birthday smalldatetime
set @Birthday ='1949-10-01'

--多用一个日期变量,为了扩展: 计算将来或过去某一时刻的当时的精确年龄 的功能!
declare @ smalldatetime
set @ = getdate()

select datediff(year,@Birthday,getdate())
       - case when datediff(day,dateadd(year,datediff(year,@Birthday,getdate()),@Birthday),getdate()) >= 0
                   then 0
              else 1
         end

T-SQL UDF:
create function GetAge(@BeginDate datetime,@EndDate datetime)
returns integer
begin
return (select datediff(year,@BeginDate,@EndDate) - case when datediff(day,dateadd(year,datediff(year,@BeginDate,@EndDate),@BeginDate),@EndDate) >= 0 then 0 else 1 end)
end

再说第一个:
1.计算任意两日期之间的 周一 到 周日 分别各有几个!
   起因来自该贴:
  《SQL语句求当月的上班天数》
   http://community.csdn.net/Expert/TopicView1.asp?id=3291510

   本来想偷懒 Google 一下,没有满意的结果,好像他自己也没底!思路也不一样!
  《求一尖酸的算法》(有点儿同感,说起来简单,做起来就晕)
   http://www.itpub.net/252645.html

   顺便说一下,我写程序总想写的"更通用"一些,可能答非所问了!


由于 @@datefirst 可通过 set datefirst N 设定 导致 datepart(weekday,[date]) 不确定!
所以用 datename!
因为确定的日期是周几肯定是永远不变的!当然不随 datefirst 变!
所以更要用 datename!
另外按着中国人的习惯: 周日算作上周的最后一天!
如果是其他语言版本的 SQL Server 注意选用该语言版本的 周几 的形式!

declare @b datetime
declare @e datetime

set @b = '2004-07-29'
set @e = '2004-08-05'

select @b as 开始日期,@e as 结束日期,
datediff(week
         ,case when datename(weekday,@b) = '星期日' then @b - 1
               else @b end
         ,case when datename(weekday,@e) = '星期日' then @e - 1
               else @e end
        ) + 1 as 跨周数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期二','星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
as 周一个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一') then 1
       else 0 end
as 周二个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二') then 1
       else 0 end
as 周三个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三') then 1
       else 0 end
as 周四个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四') then 1
       else 0 end
as 周五个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五') then 1
       else 0 end
as 周六个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五','星期六') then 1
       else 0 end

as 周日个数

--哇塞终于搞定了,起初没想到的是: 竟然着实动了一番脑筋!

剩下的就是用的人可以为所欲为地将他变成 UDF SP (View) 等了!
如果你真正理解了我的思路,就可以用任何一种语言实现!

最后再作一下广告:
http://www.microshaoft.com
http://www.csdn.net/Develop/list_article.asp?author=playyuer

送你几个巨长的 SQL,也许狠有用,点一下,帮我加点儿人气:

T-SQL 生成 两个新的真正的公历年历
http://www.csdn.net/Develop/Read_Article.asp?Id=26447


T-SQL 生成一个简易的 公历年历 T-SQL 含日期所在月及年的周次
http://www.csdn.net/Develop/Read_Article.asp?Id=26083


http://www.microshaoft.com
http://www.csdn.net/Develop/list_article.asp?author=playyuer

截止到2004-08-21 :

一共三板斧:
1,2 是 邹建的

邹建的想法就是截头去尾
因为datediff不受 set datefirst 的影响,固定一周从周日到周六
所以我的想法也就是通过 @@datefirst 与 datepart(weekday,@dt) 的值,换算成固定的每周从0(星期天)到6(星期六)的数字,按下面的规则进行统计

datediff(week,@dt_begin,@dt_end) 的结果
如果是为0,表示没有跨周
那么统计的时候,就从 @dt_begin 到 @dt_end 之间的星期x有数,其他都没有数

如果是为1,表示跨周,但两个周都不完整
那么统计的时候,就分别统计
    @dt_begin 到周六的星期X的数
   +周日到 @dt_end 之间的星期x的数

如果是>1,表示跨周,而且中间有完整的周
那么统计的时候,就分别统计
    @dt_begin 到周六的星期X的数
   +周日到 @dt_end 之间的星期x的数
   +完整的周数

declare @b datetime,@e datetime
select @b='2002-8-5',@e='2008-7-15'

--查询方式1(纵向)
select
        项目='跨周数'
 ,值=case when @b<@e
  then (datediff(day,@b,@e)+7)/7
  else (datediff(day,@e,@b)+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 @b<@e
  then datediff(week,@b,@e)-1
  else datediff(week,@e,@b)-1 end
 ,b=case when @b<@e
  then (@@datefirst+datepart(weekday,@b)-1)%7
  else (@@datefirst+datepart(weekday,@e)-1)%7 end
 ,c=case when @b<@e
  then (@@datefirst+datepart(weekday,@e)-1)%7
  else (@@datefirst+datepart(weekday,@b)-1)%7 end
 )b


--查询方式2(横向)
select  @b as 开始日期,@e as 结束日期
        ,跨周数
 ,周一=case a
  when -1 then case when b=1 then 1 else 0 end
  when  0 then case when b<=1 then 1 else 0 end
    +case when c>=1 then 1 else 0 end
  else a+case when b<=1 then 1 else 0 end
   +case when c>=1 then 1 else 0 end
  end
 ,周二=case a
  when -1 then case when b=2 then 1 else 0 end
  when  0 then case when b<=2 then 1 else 0 end
    +case when c>=2 then 1 else 0 end
  else a+case when b<=2 then 1 else 0 end
   +case when c>=2 then 1 else 0 end
  end
 ,周三=case a
  when -1 then case when b=3 then 1 else 0 end
  when  0 then case when b<=3 then 1 else 0 end
    +case when c>=3 then 1 else 0 end
  else a+case when b<=3 then 1 else 0 end
   +case when c>=3 then 1 else 0 end
  end
 ,周四=case a
  when -1 then case when b=4 then 1 else 0 end
  when  0 then case when b<=4 then 1 else 0 end
    +case when c>=4 then 1 else 0 end
  else a+case when b<=4 then 1 else 0 end
   +case when c>=4 then 1 else 0 end
  end
 ,周五=case a
  when -1 then case when b=5 then 1 else 0 end
  when  0 then case when b<=5 then 1 else 0 end
    +case when c>=5 then 1 else 0 end
  else a+case when b<=5 then 1 else 0 end
   +case when c>=5 then 1 else 0 end
  end
 ,周六=case a
  when -1 then case when b=6 then 1 else 0 end
  when  0 then case when b<=6 then 1 else 0 end
    +case when c>=6 then 1 else 0 end
  else a+case when b<=6 then 1 else 0 end
   +case when c>=6 then 1 else 0 end
  end
 ,周日=case a
  when -1 then case when b=0 then 1 else 0 end
  when  0 then case when b<=0 then 1 else 0 end
    +case when c>=0 then 1 else 0 end
  else a+case when b<=0 then 1 else 0 end
   +case when c>=0 then 1 else 0 end
  end
from(
 select 跨周数=case when @b<@e
   then (datediff(day,@b,@e)+7)/7
   else (datediff(day,@e,@b)+7)/7 end
  ,a=case when @b<@e
   then datediff(week,@b,@e)-1
   else datediff(week,@e,@b)-1 end
  ,b=case when @b<@e
   then (@@datefirst+datepart(weekday,@b)-1)%7
   else (@@datefirst+datepart(weekday,@e)-1)%7 end
  ,c=case when @b<@e
   then (@@datefirst+datepart(weekday,@e)-1)%7
   else (@@datefirst+datepart(weekday,@b)-1)%7 end)a

--3 是 playyuer 的

select @b as 开始日期,@e as 结束日期,
datediff(week
         ,case when datename(weekday,@b) = '星期日' then @b - 1
               else @b end
         ,case when datename(weekday,@e) = '星期日' then @e - 1
               else @e end
        ) + 1 as 跨周数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期二','星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
as 周一个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一') then 1
       else 0 end
as 周二个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二') then 1
       else 0 end
as 周三个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三') then 1
       else 0 end
as 周四个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四') then 1
       else 0 end
as 周五个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五') then 1
       else 0 end
as 周六个数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五','星期六') then 1
       else 0 end

as 周日个数

--======================
2004-09-11 再次汇总发现
--===========

declare @b datetime,@e datetime
set @b = '2004-9-14'
set @e = '2004-9-15'


select
 @b as 开始日期
,@e as 结束日期
,datediff(week,@b,@e) + 1
+ case when (@@datefirst + datepart(weekday,@b)) % 7 = 1 then 1 else 0 end
- case when (@@datefirst + datepart(weekday,@e)) % 7 = 1 then 1 else 0 end as 跨周数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 2 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 2 then 0 else 1 end as 周一数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 3 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 3 then 0 else 1 end as 周二数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 4 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 4 then 0 else 1 end as 周三数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 5 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 5 then 0 else 1 end as 周四数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 6 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 6 then 0 else 1 end as 周五数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 7 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 7 then 0 else 1 end as 周六数
,datediff(week,@b,@e) + case when (@@datefirst + datepart(weekday,@b)) % 7 + case when (@@datefirst + datepart(weekday,@b)) % 7 = 0 then 7 else 0 end > 1 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@e)) % 7 + case when (@@datefirst + datepart(weekday,@e)) % 7 = 0 then 7 else 0 end >= 1 then 0 else 1 end as 周日数

select @b as 开始日期,@e as 结束日期,
datediff(week
         ,case when datename(weekday,@b) = '星期日' then @b - 1
               else @b end
         ,case when datename(weekday,@e) = '星期日' then @e - 1
               else @e end
        ) + 1 as 跨周数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期二','星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
as 周一数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期三','星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一') then 1
       else 0 end
as 周二数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期四','星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二') then 1
       else 0 end
as 周三数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期五','星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三') then 1
       else 0 end
as 周四数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期六','星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四') then 1
       else 0 end
as 周五数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@b) in ('星期日') then 1
       else 0 end
- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五') then 1
       else 0 end
as 周六数

      ,datediff(week
                 ,case when datename(weekday,@b) = '星期日' then @b - 1
                       else @b end
                 ,case when datename(weekday,@e) = '星期日' then @e - 1
                       else @e end
                ) + 1

- case when datename(weekday,@e) in ('星期一','星期二','星期三','星期四','星期五','星期六') then 1
       else 0 end

as 周日数

 

--查询方式2(横向)
select  @b as 开始日期,@e as 结束日期
        ,跨周数
 ,周一数=case a
  when -1 then case when 1 between b and c then 1 else 0 end
  when  0 then case when b<=1 then 1 else 0 end
    +case when c>=1 then 1 else 0 end
  else a+case when b<=1 then 1 else 0 end
   +case when c>=1 then 1 else 0 end
  end
 ,周二数=case a
  when -1 then case when 2 between b and c then 1 else 0 end
  when  0 then case when b<=2 then 1 else 0 end
    +case when c>=2 then 1 else 0 end
  else a+case when b<=2 then 1 else 0 end
   +case when c>=2 then 1 else 0 end
  end
 ,周三数=case a
  when -1 then case when 3 between b and c then 1 else 0 end
  when  0 then case when b<=3 then 1 else 0 end
    +case when c>=3 then 1 else 0 end
  else a+case when b<=3 then 1 else 0 end
   +case when c>=3 then 1 else 0 end
  end
 ,周四数=case a
  when -1 then case when 4 between b and c then 1 else 0 end
  when  0 then case when b<=4 then 1 else 0 end
    +case when c>=4 then 1 else 0 end
  else a+case when b<=4 then 1 else 0 end
   +case when c>=4 then 1 else 0 end
  end
 ,周五数=case a
  when -1 then case when 5 between b and c then 1 else 0 end
  when  0 then case when b<=5 then 1 else 0 end
    +case when c>=5 then 1 else 0 end
  else a+case when b<=5 then 1 else 0 end
   +case when c>=5 then 1 else 0 end
  end
 ,周六数=case a
  when -1 then case when 6 between b and c then 1 else 0 end
  when  0 then case when b<=6 then 1 else 0 end
    +case when c>=6 then 1 else 0 end
  else a+case when b<=6 then 1 else 0 end
   +case when c>=6 then 1 else 0 end
  end
 ,周日数=case a
  when -1 then case when 0 between b and c then 1 else 0 end
  when  0 then case when b<=0 then 1 else 0 end
    +case when c>=0 then 1 else 0 end
  else a+case when b<=0 then 1 else 0 end
   +case when c>=0 then 1 else 0 end
  end
from(
 select 跨周数=case when @b<@e
   then (datediff(day,@b,@e)+7)/7
   else (datediff(day,@e,@b)+7)/7 end
  ,a=case when @b<@e
   then datediff(week,@b,@e)-1
   else datediff(week,@e,@b)-1 end
  ,b=case when @b<@e
   then (@@datefirst+datepart(weekday,@b)-1)%7
   else (@@datefirst+datepart(weekday,@e)-1)%7 end
  ,c=case when @b<@e
   then (@@datefirst+datepart(weekday,@e)-1)%7
   else (@@datefirst+datepart(weekday,@b)-1)%7 end)a

 

select
        项目='跨周数'
 ,值=case when @b<@e
  then (datediff(day,@b,@e)+7)/7
  else (datediff(day,@e,@b)+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 @b<@e
  then datediff(week,@b,@e)-1
  else datediff(week,@e,@b)-1 end
 ,b=case when @b<@e
  then (@@datefirst+datepart(weekday,@b)-1)%7
  else (@@datefirst+datepart(weekday,@e)-1)%7 end
 ,c=case when @b<@e
  then (@@datefirst+datepart(weekday,@e)-1)%7
  else (@@datefirst+datepart(weekday,@b)-1)%7 end
 )b

T-SQL UDF:
create function WeekdayDiff(@Weekday integer,@BeginDate datetime,@EndDate datetime)
-- @Weekday:  1= Monday , ... ,7= Sunday
returns integer
begin
return (select datediff(week,@BeginDate,@EndDate) + case when (@@datefirst + datepart(weekday,@BeginDate)) % 7 + case when (@@datefirst + datepart(weekday,@BeginDate)) % 7 = 0 then 7 else 0 end > @Weekday % 7 + 1 then 0 else 1 end - case when (@@datefirst + datepart(weekday,@EndDate)) % 7 + case when (@@datefirst + datepart(weekday,@EndDate)) % 7 = 0 then 7 else 0 end >= @Weekday % 7 + 1 then 0 else 1 end)
end

create  function Weekday(@Date datetime)
returns integer
begin
--return: 1= Monday , ... ,7= Sunday
return (select (@@datefirst + datepart(weekday,@Date)) % 7
        + case when (@@datefirst + datepart(weekday,@Date)) % 7 < 2
                    then 6
               else -1
          end)
end

Java 根据年月日精确计算年龄

虽说网上好多根据年月日计算年龄的,但是有的是粗略计算年龄,有的就没有防护未来的日期,以至于年龄存在负数的情况。 so,自己动手丰衣足食,写了以下工具类: package com.jglz.qing.a...
  • qq_33237207
  • qq_33237207
  • 2017年03月28日 14:42
  • 4804

Oracle取上周一到周末日期的查询语句

Oracle取上周一到周末日期的查询语句 -- Oracle 取上周一到周末的sql      -- 这样取的是 在一周内第几天,是以周日为开始的  select to...
  • doupeihua
  • doupeihua
  • 2016年04月28日 10:39
  • 1874

Java 计算时间差之年龄问题

年月日天数计算。
  • wenbitianxiafeng
  • wenbitianxiafeng
  • 2015年05月19日 15:23
  • 2395

根据身份证号,使用Java编写程序获取年龄、性别、出生日期

程序员必须要有一个好的思想,代码有时候就体现了一个人的灵魂,所以理解需求比技术更重要! IdcardValidator类 import java.text.ParseException; im...
  • dabing69221
  • dabing69221
  • 2013年06月22日 21:52
  • 17563

java获取当前时间所在一周的周一和周日日期

/** * 当前时间所在一周的周一和周日时间 * @param time 当前时间 * @return */ public static Map get...
  • Blue_Red_1314
  • Blue_Red_1314
  • 2017年11月22日 11:52
  • 265

Js年龄计算,精确到岁、月、小时

公司做医疗,需要根据时间获取精确年龄(主要是儿童年龄),精确度要求较高,从网上找了好久都不符合要求,或者只是精确到岁,或者跨月时,天数误差过大,没办法只好自己写了一分,请大家指点 需求: 1 ...
  • dreamtyz
  • dreamtyz
  • 2016年01月27日 01:13
  • 589

iOS 获取当前时间以及计算年龄(时间差)

NSDate *now = [NSDate date]; NSLog(@”now date is: %@”, now); NSCalendar *calend...
  • hsf_study
  • hsf_study
  • 2016年08月17日 17:33
  • 1218

根据当前日期,获得本周的周一及周日日期即本周的开始时间和结束时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式 Calendar cal = Calendar.ge...
  • Elise1
  • Elise1
  • 2017年05月08日 10:50
  • 767

mysql 对取当前日期周一和周日语句的详细解析

文章出处:mysql 对取当前日期周一和周日语句的详细解析 网上对于用mysql取当前日期周一和周日的方法非常多,但基本上都只有一个方法,没有什么解释,后果就是会用,但为什么要这样用,就不一定知道...
  • woshizhangliang999
  • woshizhangliang999
  • 2015年08月26日 21:16
  • 3421

android 通过datepicker计算年龄

界面如下,凑合看吧 点击选择年龄的按钮时弹出datepicker日历选择器 在日历选择器上选中您的年龄点击完成 activity_main.xml的代码如...
  • rosener
  • rosener
  • 2016年09月06日 09:45
  • 1370
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:T-SQL 2 Tips: 1.计算任意两日期之间的"周一"到"周日"分别各有几个! 2.根据出生日期计算精确年龄!
举报原因:
原因补充:

(最多只允许输入30个字)