相思资源网 Design By www.200059.com
一、给定一个日期值,求出此日期所在星期的星期一和星期天的日期数据
例如给定一个日期 2010-09-01,求出它所在星期的星期一是2010-08-30,星期天是2010-09-05
Function创建如下:
复制代码 代码如下:
USE [MSSQL]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[My_OneDay_GetWeekFirstAndEndDay](@tmpDate DATETIME)
RETURNS @tmpTable TABLE(FirstDay DATETIME , EndDay DATETIME)
AS
BEGIN
INSERT INTO @tmpTable
SELECT a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy
) a
LEFT JOIN (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay
) b
ON a.ID = b.ID
RETURN
End
Function测试:
复制代码 代码如下:
SELECT * from My_OneDay_GetWeekFirstAndEndDay('2010-09-01')
二、以上面单个日期搜索为基础,由用户输入两个参数,一个是开始日期,一个结束日期,根据这两个参数,求出在此时期段内的所有星期的星期一和星期天的日期表并排序。
例如开始日期是2011-09-01,结束日期是2011-10-06,我们就可以得到此星期表如下:
WeekOrder FirstDay EndDay
1 2011-08-29 00:00:00.000 2011-09-04 00:00:00.000
2 2011-09-05 00:00:00.000 2011-09-11 00:00:00.000
3 2011-09-12 00:00:00.000 2011-09-18 00:00:00.000
4 2011-09-19 00:00:00.000 2011-09-25 00:00:00.000
5 2011-09-26 00:00:00.000 2011-10-02 00:00:00.000
6 2011-10-03 00:00:00.000 2011-10-09 00:00:00.000
Function创建如下:
复制代码 代码如下:
USE [MSSQL]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[MY_Range_GetWeekFirstAndEndDays](@tmpDateSTART DATETIME,@tmpDateEND DATETIME)
RETURNS @tmpTable TABLE(WeekOrder INT,FirstDay DATETIME , EndDay DATETIME)
AS
BEGIN
DECLARE @tmpDate DATETIME
DECLARE @index INT
SET @tmpDate=@tmpDateSTART
SET @index=1
WHILE @tmpDate <=@tmpDateEND
BEGIN
INSERT INTO @tmpTable
SELECT @index,a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy) a
LEFT JOIN (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay) b
ON a.ID = b.ID
SET @tmpDate=DATEADD(DAY,7,@tmpDate)
SET @index=@index+1
END
RETURN
End
Function测试:
复制代码 代码如下:
SELECT * from My_Range_GetWeekFirstAndEndDays('2011-09-01','2011-10-06')
例如给定一个日期 2010-09-01,求出它所在星期的星期一是2010-08-30,星期天是2010-09-05
Function创建如下:
复制代码 代码如下:
USE [MSSQL]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[My_OneDay_GetWeekFirstAndEndDay](@tmpDate DATETIME)
RETURNS @tmpTable TABLE(FirstDay DATETIME , EndDay DATETIME)
AS
BEGIN
INSERT INTO @tmpTable
SELECT a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy
) a
LEFT JOIN (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay
) b
ON a.ID = b.ID
RETURN
End
Function测试:
复制代码 代码如下:
SELECT * from My_OneDay_GetWeekFirstAndEndDay('2010-09-01')
二、以上面单个日期搜索为基础,由用户输入两个参数,一个是开始日期,一个结束日期,根据这两个参数,求出在此时期段内的所有星期的星期一和星期天的日期表并排序。
例如开始日期是2011-09-01,结束日期是2011-10-06,我们就可以得到此星期表如下:
WeekOrder FirstDay EndDay
1 2011-08-29 00:00:00.000 2011-09-04 00:00:00.000
2 2011-09-05 00:00:00.000 2011-09-11 00:00:00.000
3 2011-09-12 00:00:00.000 2011-09-18 00:00:00.000
4 2011-09-19 00:00:00.000 2011-09-25 00:00:00.000
5 2011-09-26 00:00:00.000 2011-10-02 00:00:00.000
6 2011-10-03 00:00:00.000 2011-10-09 00:00:00.000
Function创建如下:
复制代码 代码如下:
USE [MSSQL]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[MY_Range_GetWeekFirstAndEndDays](@tmpDateSTART DATETIME,@tmpDateEND DATETIME)
RETURNS @tmpTable TABLE(WeekOrder INT,FirstDay DATETIME , EndDay DATETIME)
AS
BEGIN
DECLARE @tmpDate DATETIME
DECLARE @index INT
SET @tmpDate=@tmpDateSTART
SET @index=1
WHILE @tmpDate <=@tmpDateEND
BEGIN
INSERT INTO @tmpTable
SELECT @index,a.FirstDay,b.EndDay FROM (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy) a
LEFT JOIN (
SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay) b
ON a.ID = b.ID
SET @tmpDate=DATEADD(DAY,7,@tmpDate)
SET @index=@index+1
END
RETURN
End
Function测试:
复制代码 代码如下:
SELECT * from My_Range_GetWeekFirstAndEndDays('2011-09-01','2011-10-06')
标签:
日期,星期
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无SQL 判断给定日期值(或时间段)所在星期的星期一和星期天的日期的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。