1. 程式人生 > >日歷 存儲過程

日歷 存儲過程

scrip order com null date() sunday str gin 效果

生成的日歷的存儲過程

本文轉自:http://www.cnblogs.com/yuylyp/archive/2009/11/08/1598485.html

效果圖:

技術分享

第一種:

CREATE PROCEDURE [dbo].[GetCalendar]
(
@InputDateTime DATE = NULL
)
AS
BEGIN
IF @InputDateTime IS NULL
SET @InputDateTime = GETDATE() ;

WITH x ( Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Week, Date )

AS ( SELECT CASE WHEN DATEPART(dw, thismonth.first) = 1
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 2
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 3
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 4
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 5
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 6
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
CASE WHEN DATEPART(dw, thismonth.first) = 7
THEN DAY(thismonth.FIRST)
ELSE NULL
END ,
DATEPART(WK, thismonth.first) ,
thismonth.first
FROM ( SELECT DATEADD(D,
-DATEPART(d,
@InputDateTime)
+ 1, @InputDateTime) AS first
) AS thismonth
UNION ALL
SELECT CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 1
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 2
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 3
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 4
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 5
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 6
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
CASE WHEN DATEPART(dw, DATEADD(d, 1, x.Date)) = 7
THEN DAY(DATEADD(d, 1, x.Date))
ELSE NULL
END ,
DATEPART(WK, DATEADD(d, 1, x.Date)) ,
DATEADD(d, 1, x.Date)
FROM x
WHERE MONTH(DATEADD(d, 1, x.Date)) = MONTH(x.Date)
)
SELECT MAX(x.Sunday) AS Sunday ,
MAX(x.Monday) AS Monday ,
MAX(x.Tuesday) AS Tuesday ,
MAX(x.Wednesday) AS Wednesday ,
MAX(x.Thursday) AS Thursday ,
MAX(x.Friday) AS Friday ,
MAX(x.Saturday) AS Saturday
FROM x
GROUP BY Week
ORDER BY Week
END

GO

第二種

USE [ShiBei]
GO

/****** Object: StoredProcedure [dbo].[GetCalendar2] Script Date: 2017/7/31 17:59:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[GetCalendar2] ( @input DATE = NULL )
AS
BEGIN
IF @input IS NULL
SET @input = GETDATE() ;

WITH xx ( Day, WeekDay, Week, Date )
AS ( SELECT DAY(tt.Date) ,
DATEPART(dw, tt.Date) ,
DATEPART(ww, tt.Date) ,
tt.Date
FROM ( SELECT DATEADD(d, -DAY(@input) + 1,
@input) AS Date
) AS tt
UNION ALL
SELECT xx.day + 1 ,
DATEPART(dw, DATEADD(d, 1, xx.Date)) ,
DATEPART(ww, DATEADD(d, 1, xx.Date)) ,
DATEADD(d, 1, xx.date)
FROM xx
WHERE DATEPART(m, DATEADD(d, 1, xx.Date)) = DATEPART(m,
xx.Date)
)
SELECT [1] AS Sunday ,
[2] AS Monday ,
[3] AS Tuesday ,
[4] AS Wednesday ,
[5] AS Thursday ,
[6] AS Friday ,
[7] AS Saturday
FROM ( SELECT xx.day ,
xx.WeekDay ,
xx.Week
FROM xx
) AS p PIVOT( MAX(p.Day) FOR p.WeekDay IN ( [1], [2], [3],
[4], [5], [6],
[7] ) ) AS pvt
ORDER BY Week ASC
END

GO

日歷 存儲過程