程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> SQL 斷定給定日期值(或時光段)地點禮拜的禮拜一和禮拜天的日期

SQL 斷定給定日期值(或時光段)地點禮拜的禮拜一和禮拜天的日期

編輯:MSSQL

SQL 斷定給定日期值(或時光段)地點禮拜的禮拜一和禮拜天的日期。本站提示廣大學習愛好者:(SQL 斷定給定日期值(或時光段)地點禮拜的禮拜一和禮拜天的日期)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL 斷定給定日期值(或時光段)地點禮拜的禮拜一和禮拜天的日期正文


1、給定一個日期值,求出此日期地點禮拜的禮拜一和禮拜天的日期數據
例如給定一個日期 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')

2、以下面單個日期搜刮為基本,由用戶輸出兩個參數,一個是開端日期,一個停止日期,依據這兩個參數,求出在此時代段內的一切禮拜的禮拜一和禮拜天的日期表並排序。
例如開端日期是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')
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved