首页 > 开发 > 综合 > 正文

T-SQL: 15 个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受 @@Dat

2024-07-21 02:05:55
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,
/*
t-sql: 15 个与日期时间相关的自定义函数(udf),周日作为周的最后一天,均不受 @@datefirst、语言版本影响
都是从老文章里收集或提炼出来的!
提示:
(@@datefirst + datepart(weekday,@date)) % 7 判断周几是最保险的! 与 @@datefirst 无关,与语言版本无关
@@datefirst 可能会导致 datepart(weekday,@date) 不一样!
无论 @@datefirst 等于几,无论是什么语言版本的 sql server 下面永远恒成立!
(@@datefirst + datepart(weekday,@date)): 2、3、4、5、6、0、1 分别代表 周一 到 周日
-- */

create function udf_getage(@startdate datetime,@enddate datetime)
returns integer
-- 返回精确年龄 select dbo.udf_getage('1949-10-01',getdate())
begin
return datediff(year,@startdate,@enddate)
       - case when datediff(day,dateadd(year,datediff(year,@startdate,@enddate),@startdate),@enddate) >= 0
                   then 0
              else
                   1
         end
end

go

create function udf_daysofyearbydate(@date datetime)
returns integer
-- 返回年的天数 可判断 平(365)、润(366) 年
begin
return datediff(day,dateadd(year,datediff(year,0,@date),0),dateadd(year,datediff(year,0,@date )+1,0))
end

go

create function udf_daysofyear(@year integer)
returns integer
-- 返回年的天数 可判断 平(365)、润(366) 年
begin
return datediff(day,dateadd(year,@year - year(0),0),dateadd(year,@year - year(0) + 1,0))
end

go

create function udf_halfday(@date datetime)
returns datetime
-- 返回 @date 是 上午 返回 @date 的零点,@date 是 下午 返回 @date 的十二点
as
begin
return case when datepart(hour,@date) < 12
                 then dateadd(day,datediff(day,0,@date),0) --上午归到 零点
            else
                 dateadd(hour,12,dateadd(day,datediff(day,0,@date),0)) --下午归到 十二点
       end
end

go

create function udf_weekdiff(@startdate datetime,@enddate datetime)
returns integer
-- 返回 [@startdate , @enddate] 之间周数 周日是当周的最后一天
begin
return datediff(week,@startdate,@enddate) -- + 1
       + case when (@@datefirst + datepart(weekday,@startdate)) % 7 = 1
                   then 1
              else
                   0
         end
       - case when (@@datefirst + datepart(weekday,@enddate)) % 7 = 1
                   then 1
              else 0
         end
end

go

create function udf_weekofmonth(@date datetime)
-- 返回 @date 是所在月的第几周 周日是当周的最后一天
returns integer
as
begin
return datediff(week
                ,case when (@@datefirst + datepart(weekday,dateadd(month,datediff(month,0,@date),0))) % 7 = 1
                           then dateadd(month,datediff(month,0,@date),0) - 1
                      else
                           dateadd(month,datediff(month,0,@date),0)
                      end
                ,case when (@@datefirst + datepart(weekday,@date)) % 7 = 1
                           then @date-1
                      else @date
                 end
               ) + 1
end

go

create function udf_weekofquarter(@date datetime)
-- 返回 @date 是所在季度的第几周 周日是当周的最后一天
returns int
as
begin
return datediff(week
                ,case when (@@datefirst + datepart(weekday,dateadd(quarter,datediff(quarter,0,@date),0))) % 7 = 1
                           then dateadd(quarter,datediff(quarter,0,@date),0) - 1
                      else
                           dateadd(quarter,datediff(quarter,0,@date),0)
                 end
                ,case when (@@datefirst + datepart(weekday,@date)) % 7 = 1
                           then @date - 1
                      else
                           @date
                 end
               ) + 1
end

go

create function udf_weekofyear(@date datetime)
-- 返回 @date 是所在年的第几周 周日是当周的最后一天
returns int
as
begin
return datediff(week
                ,case when (@@datefirst + datepart(weekday,dateadd(day,0,datediff(day,0,dateadd(year,datediff(year,0,@date),0))))) % 7 = 1
                           then dateadd(day,-1,dateadd(day,0,datediff(day,0,dateadd(year,datediff(year,0,@date),0))))
                      else
                           dateadd(day,0,datediff(day,0,dateadd(year,datediff(year,0,@date),0))) --date 所在年的第一天 即: 一月一号
                 end
                ,case when (@@datefirst + datepart(weekday,@date)) % 7 = 1
                           then dateadd(day,-1,@date)
                      else
                           @date
                 end
               ) + 1
end

go

create function udf_weekday(@ int,@date datetime)
returns datetime
-- 返回 @date 映射到 所在周的其他天 周日是当周的最后一天
begin
/*
--周日算作(上一)周的最后一天
 当 @ <= 1 代表将 @date 映射到 所在周的星期一
 当 @ = 2 代表将 @date 映射到 所在周的星期二
 当 @ = 3 代表将 @date 映射到 所在周的星期三
 当 @ = 4 代表将 @date 映射到 所在周的星期四
 当 @ = 5 代表将 @date 映射到 所在周的星期五
 当 @ = 6 代表将 @date 映射到 所在周的星期六
 当 @ >= 7 代表将 @date 映射到 所在周的星期日
 可用于按周汇总 group by,均支持跨年跨月数据
*/
return dateadd(day
               ,case when (@@datefirst + datepart(weekday,@date)) % 7 = 0 --周六
                          then case when @ between 1 and 6
                                         then @ - 6
                                    else
                                         1
                               end
                     when (@@datefirst + datepart(weekday,@date)) % 7 = 1 --周日(七)
                          then case when @ between 1 and 6
                                         then @ - 7
                                    else
                                         0
                               end
                     when (@@datefirst + datepart(weekday,@date)) % 7 between 2 and 6 --周一至周五
                          then case when @ between 1 and 6
                                         then @ + 1 - (@@datefirst + datepart(weekday,@date)) % 7
                                    else
                                         8 - (@@datefirst + datepart(weekday,@date)) % 7
                               end
                end
               ,@date)
end

go

create function udf_weekdaydiff(@weekday integer,@startdate datetime,@enddate datetime)
returns integer
-- -- 返回 [@startdate , @enddate] 之间周一 到 周日的个数 周日是当周的最后一天
begin
-- @weekday: 1: monday , ... ,7: sunday
return datediff(week,@startdate,@enddate)
       + case when (@@datefirst + datepart(weekday,@startdate)) % 7
                   + case when (@@datefirst + datepart(weekday,@startdate)) % 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
/* test:

declare @b datetime
declare @e datetime

set @b = '2004-01-29'
set @e = '2004-09-05'

select @b as begindate ,@e as enddate
,dbo.udf_weekdaydiff(1,@b,@e) as countofmonday
,dbo.udf_weekdaydiff(2,@b,@e) as countoftuesday
,dbo.udf_weekdaydiff(3,@b,@e) as countofwednesday
,dbo.udf_weekdaydiff(4,@b,@e) as countofthursday
,dbo.udf_weekdaydiff(5,@b,@e) as countoffriday
,dbo.udf_weekdaydiff(6,@b,@e) as countofsaturday
,dbo.udf_weekdaydiff(7,@b,@e) as countofsunday
*/
end

go

create function udf_weekdayid(@date datetime)
returns integer
-- 返回 @date 是 monday 返回 1, ... ,是 sunday 返回 1
begin
--1: monday , ... ,7: sunday
return (@@datefirst + datepart(weekday,@date)) % 7
       + case when (@@datefirst + datepart(weekday,@date)) % 7 < 2
                   then 6
              else
                   -1
         end
end

go

create function udf_nextworkdate(@date datetime)
returns datetime
-- 返回 @date 的下一个工作日
begin
/*
declare @i int
set @i = 3
declare @date datetime
set @date = '2005-01-02'
-- */
return case when (@@datefirst + datepart(weekday,@date)) % 7 = 6 -- friday
                 then dateadd(day,3,@date)
            when (@@datefirst + datepart(weekday,@date)) % 7 = 0 -- saturday
                 then dateadd(day,2,@date)
            else
                 dateadd(day,1,@date)
       end
end

go

create function udf_previousworkdate(@date datetime)
returns datetime
-- 返回 @date 的上一个工作日
begin
/*
declare @i int
set @i = 3
declare @date datetime
set @date = '2005-01-02'
-- */
return case when (@@datefirst + datepart(weekday,@date)) % 7 = 2 -- monday
                 then dateadd(day,-3,@date)
            when (@@datefirst + datepart(weekday,@date)) % 7 = 1 -- sunday
                 then dateadd(day,-2,@date)
            else
                 dateadd(day,-1,@date)
       end
end

go

create function udf_workdateadd(@i integer,@date datetime)
returns datetime
-- 返回 @date 加上一段 @i 个工作日的新值
begin
declare @ int
set @ = 0
while @ < abs(@i)
begin
   set @date = case when @i >= 0
                         then --dbo.udf_nextworkdate(@date)
                              case when (@@datefirst + datepart(weekday,@date)) % 7 = 6 -- friday
                                        then dateadd(day,3,@date)
                                   when (@@datefirst + datepart(weekday,@date)) % 7 = 0 -- saturday
                                        then dateadd(day,2,@date)
                                   else
                                        dateadd(day,1,@date)
                              end
                    else
                         --dbo.udf_previousworkdate(@date)
                         case when (@@datefirst + datepart(weekday,@date)) % 7 = 2 -- monday
                                   then dateadd(day,-3,@date)
                              when (@@datefirst + datepart(weekday,@date)) % 7 = 1 -- sunday
                                   then dateadd(day,-2,@date)
                              else
                                   dateadd(day,-1,@date)
                         end
               end
               set @ = @ + 1
end
return @date
end

go

create function udf_getstar (@ datetime)
returns varchar(100)
-- 返回日期所属星座
begin
return
(
--declare @ datetime
--set @ = getdate()
select max(star)
from
(
select '魔羯座' as star,1 as [month],1 as [day]
union all select '水瓶座',1,20
union all select '双鱼座',2,19
union all select '牡羊座',3,21
union all select '金牛座',4,20
union all select '双子座',5,21
union all select '巨蟹座',6,22
union all select '狮子座',7,23
union all select '处女座',8,23
union all select '天秤座',9,23
union all select '天蝎座',10,24
union all select '射手座',11,22
union all select '魔羯座',12,22
) stars
where dateadd(month,[month] - 1,dateadd(year,year(@) - year(0),0)) + [day] - 1 =
(
select max(dateadd(month,[month] - 1,dateadd(year,year(@) - year(0),0)) + [day] - 1)
from (
select '魔羯座' as star,1 as [month],1 as [day]
union all select '水瓶座',1,20
union all select '双鱼座',2,19
union all select '牡羊座',3,21
union all select '金牛座',4,20
union all select '双子座',5,21
union all select '巨蟹座',6,22
union all select '狮子座',7,23
union all select '处女座',8,23
union all select '天秤座',9,23
union all select '天蝎座',10,24
union all select '射手座',11,22
union all select '魔羯座',12,22
) stars
where @ >= dateadd(month,[month] - 1,dateadd(year,year(@) - year(0),0)) + [day] - 1
)
)
end
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表