T-SQL:15个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受@@DateFirst、语言版本影响
来源:互联网 发布:富士钓具淘宝旗舰店 编辑:程序博客网 时间:2024/06/01 07:27
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/*
T-SQL:15个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受@@DateFirst、语言版本影响
都是从老文章里收集或提炼出来的!
提示:
(@@DateFirst+datepart(weekday,@Date))%7判断周几是最保险的!与@@DateFirst无关,与语言版本无关
@@DateFirst可能会导致datepart(weekday,@Date)不一样!
无论@@DateFirst等于几,无论是什么语言版本的下面永远恒成立!
(@@DateFirst+datepart(weekday,@Date)):2、3、4、5、6、0、1分别代表周一到周日
--*/
createfunctionUDF_GetAge(@StartDatedatetime,@EndDatedatetime)
returnsinteger
--返回精确年龄selectdbo.UDF_GetAge('1949-10-01',getdate())
begin
returndatediff(year,@StartDate,@EndDate)
-casewhendatediff(day,dateadd(year,datediff(year,@StartDate,@EndDate),@StartDate),@EndDate)>=0
then0
else
1
end
end
go
createfunctionUDF_DaysOfYearByDate(@Datedatetime)
RETURNSinteger
--返回年的天数可判断平(365)、润(366)年
begin
returndatediff(day,dateadd(year,datediff(year,0,@Date),0),dateadd(year,datediff(year,0,@Date)+1,0))
end
go
createfunctionUDF_DaysOfYear(@Yearinteger)
RETURNSinteger
--返回年的天数可判断平(365)、润(366)年
begin
returndatediff(day,dateadd(year,@year-year(0),0),dateadd(year,@year-year(0)+1,0))
end
go
createfunctionUDF_HalfDay(@Datedatetime)
returnsdatetime
--返回@Date是上午返回@Date的零点,@Date是下午返回@Date的十二点
as
begin
returncasewhendatepart(hour,@Date)<12
thendateadd(day,datediff(day,0,@Date),0)--上午归到零点
else
dateadd(hour,12,dateadd(day,datediff(day,0,@Date),0))--下午归到十二点
end
end
go
createfunctionUDF_WeekDiff(@StartDatedatetime,@EndDatedatetime)
returnsinteger
--返回[@StartDate,@EndDate]之间周数周日是当周的最后一天
begin
returndatediff(week,@StartDate,@EndDate)--+1
+casewhen(@@DateFirst+datepart(weekday,@StartDate))%7=1
then1
else
0
end
-casewhen(@@DateFirst+datepart(weekday,@EndDate))%7=1
then11
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/*
T-SQL:15个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受@@DateFirst、语言版本影响
都是从老文章里收集或提炼出来的!
提示:
(@@DateFirst+datepart(weekday,@Date))%7判断周几是最保险的!与@@DateFirst无关,与语言版本无关
@@DateFirst可能会导致datepart(weekday,@Date)不一样!
无论@@DateFirst等于几,无论是什么语言版本的下面永远恒成立!
(@@DateFirst+datepart(weekday,@Date)):2、3、4、5、6、0、1分别代表周一到周日
--*/
createfunctionUDF_GetAge(@StartDatedatetime,@EndDatedatetime)
returnsinteger
--返回精确年龄selectdbo.UDF_GetAge('1949-10-01',getdate())
begin
returndatediff(year,@StartDate,@EndDate)
-casewhendatediff(day,dateadd(year,datediff(year,@StartDate,@EndDate),@StartDate),@EndDate)>=0
then0
else
1
end
end
go
createfunctionUDF_DaysOfYearByDate(@Datedatetime)
RETURNSinteger
--返回年的天数可判断平(365)、润(366)年
begin
returndatediff(day,dateadd(year,datediff(year,0,@Date),0),dateadd(year,datediff(year,0,@Date)+1,0))
end
go
createfunctionUDF_DaysOfYear(@Yearinteger)
RETURNSinteger
--返回年的天数可判断平(365)、润(366)年
begin
returndatediff(day,dateadd(year,@year-year(0),0),dateadd(year,@year-year(0)+1,0))
end
go
createfunctionUDF_HalfDay(@Datedatetime)
returnsdatetime
--返回@Date是上午返回@Date的零点,@Date是下午返回@Date的十二点
as
begin
returncasewhendatepart(hour,@Date)<12
thendateadd(day,datediff(day,0,@Date),0)--上午归到零点
else
dateadd(hour,12,dateadd(day,datediff(day,0,@Date),0))--下午归到十二点
end
end
go
createfunctionUDF_WeekDiff(@StartDatedatetime,@EndDatedatetime)
returnsinteger
--返回[@StartDate,@EndDate]之间周数周日是当周的最后一天
begin
returndatediff(week,@StartDate,@EndDate)--+1
+casewhen(@@DateFirst+datepart(weekday,@StartDate))%7=1
then1
else
0
end
-casewhen(@@DateFirst+datepart(weekday,@EndDate))%7=1
then11
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- T-SQL:15个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受@@DateFirst、语言版本影响
- T-SQL: 17 个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受 @@DateFirst、语言版本影响
- T-SQL: 三个通用的与日期相关的,辅助按周(星期日是周的最后一天)汇总的自定义函数
- T-SQL:三个通用的与日期相关的,辅助按周(星期日是周的最后一天)汇总的自定义函数
- 15 个与日期时间相关的自定义函数
- 求日期所属星座的 T-SQL UDF (用户自定义函数)
- 到当前日期的每周一和周日、每月第一天和最后一天日期的获取
- Hive永久UDF依赖包问题以及几个日期相关的自定义函数
- 15 个与日期时间相关的精典语句函数
- 15 个与日期时间相关的精典语句函数
- 15 个与日期时间相关的精典语句函数
- 15 个与日期时间相关的精典语句函数
- 15 个与日期时间相关的精典语句函数
- PHP日期与时间相关的函数
- sql server 求以日期所在周的第一天 (星期一)和 最后一天(星期日) 函数
- 【日期时间】批处理计算上个月最后一天的日期
- sql 语句(日期所在月的第一天,最后一天)
- Sql Server的日期与时间函数
- 之父对实现关系型数据库管理系统的12条建议
- 排序:冒泡
- 国内主流PHP类CMS数据负载测评报告
- Flex2 Tree从XML文件中加载数据
- 安装PrintManager和所遇到的问题
- T-SQL:15个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受@@DateFirst、语言版本影响
- Servlet action is not available
- 用存储过程执行Insert和直接执行Insert的性能比较
- PE文件格式学习笔记
- 如何按指定的顺序获取数据
- 使用LogMiner的简单步骤
- servlet有关
- GridView绑定模板格式化日期总结
- 根据表中数据生成insert语句的存储过程