Generate a calendar table using SQL statements

Source: Internet
Author: User

Are you still using the Asp. Net control or server-side statement processing to generate a calendar table?

Or are you still using the client segment javascript to generate a calendar table?

Are the tables they generated ranked first on Sunday?

See my generated:

January February

 

January June

 

January July

 

Only a simple SQL statement (Stored Procedure) is used to input the time and output the calendar table with the first column of Monday.

From this point of view, will everyone worry about the week on the C # background?

O (∩) O Haha ~

In fact, there is nothing, is a small storage process, is not the T-SQL well, Daniel don't shoot bricks.

However, it is practical for shrimps. I posted my homepage. I have a thick face and I can't beat any bricks. If you don't believe it, try it ~

 

The stored procedure is pasted as follows:

 

Code
-- ===================================================== ======
-- Author: chf
-- Create date: 2009-06-25
-- Description: The input time. The current month list is returned.
-- ===================================================== ======
Alter PROCEDURE GetMonthTable
(
@ Date datetime
)
AS
BEGIN
DECLARE @ Start DATETIME, @ End DATETIME
DECLARE @ Index INT
SET @ Start = DATEADD (MONTH, DATEDIFF (MONTH, 0, @ Date), 0)
SET @ End = DATEADD (MONTH, 1, @ Start)
SET @ Index = DATEDIFF (DAY,-1, @ Start) % 7-1;
SET @ Start = DATEADD (mm, DATEDIFF (mm, 0, @ Date), 0)
SET @ End = DATEADD (mm, 1, @ Start)-1
SET @ Index = DATEDIFF (day, 0, @ Start) % 7

; WITH temp (date, row, col)
(
SELECT date = 1, row = @ Index/7 + 1, col = @ Index % 7 + 1
UNION ALL
SELECT date = date + 1, row = (@ Index + date)/7 + 1, col = (@ Index + date) % 7 + 1
FROM temp
WHERE date <= DATEDIFF (DAY, @ Start, @ End)
)
Select isnull (CONVERT (CHAR (2), [1]), '') AS 1,
ISNULL (CONVERT (CHAR (2), [2]), '') AS 2,
ISNULL (CONVERT (CHAR (2), [3]), '') AS 3,
ISNULL (CONVERT (CHAR (2), [4]), '') AS 4,
ISNULL (CONVERT (CHAR (2), [5]), '') AS five,
ISNULL (CONVERT (CHAR (2), [6]), '') AS 6,
ISNULL (CONVERT (CHAR (2), [7]), '') AS day
FROM temp
Bytes
(
MAX (date) FOR col IN ([1], [2], [3], [4], [5], [6], [7])
) AS B
END
GO

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.