An SQL function that generates the calendar month.

Source: Internet
Author: User
Create Function calander (@ month int, @ year INT)
Returns @ day1 table (Sun char (3), Mon char (3), Tue char (3), wed char (3), Thu char (3 ), fri char (3), sat char (3 ))
As
Begin
Declare @ I int
Declare @ J int
Declare @ intchk int
Declare @ dnum int
Declare @ curdate int
Declare @ month1 char (2)
Declare @ year1 char (4)
Declare @ date char (2)
Declare @ dtchk int
Declare @ dtval int
Set @ date = '01'
Set @ month1 = @ month
Set @ year1 = @ year
Set @ dtchk = 1
Set @ I = 1
Set @ j = 1

Declare @ day2 table (Sun char (3) default '', Mon char (3) default'', Tue char (3) default '', wed char (3) default '', Thu char (3) default'', Fri char (3) default '', sat char (3) default '')

Select @ curdate = datepart (DW, convert (datetime, @ date + '-' + @ month1 + '-' + @ year1, 103 ))

Select @ dnum = datediff (DD, convert (datetime, @ date + '-' + @ month1 + '-' + @ year1, 103), dateadd (mm, 1, convert (datetime, @ date + '-' + @ month1 + '-' + @ year1, 103 )))

While @ j <= 7
Begin
If @ curdate = @ J
Begin
If @ j = 1
Begin
Insert into @ day2 (Sun) values (@ I)
Set @ intchk = 1
Set @ dtchk = 1
End
Else if @ J = 2
Begin
Insert into @ day2 (Mon) values (@ I)
Set @ intchk = 2
Set @ dtchk = 1
End
Else if @ J = 3
Begin
Insert into @ day2 (Tue) values (@ I)
Set @ intchk = 3
Set @ dtchk = 1
End
Else if @ J = 4
Begin
Insert into @ day2 (WED) values (@ I)
Set @ intchk = 4
Set @ dtchk = 1
End
Else if @ J = 5
Begin
Insert into @ day2 (Thu) values (@ I)
Set @ intchk = 5
Set @ dtchk = 1
End
Else if @ J = 6
Begin
Insert into @ day2 (Fri) values (@ I)
Set @ intchk = 6
Set @ dtchk = 1
End
Else if @ J = 7
Begin
Insert into @ day2 (SAT) values (@ I)
Set @ intchk = 7
Set @ dtchk = 1
End
End
Set @ J = @ J + 1
End
If @ intchk = 1
Begin
Update @ day2 set MON = @ I + 1, Tue = @ I + 2, wed = @ I + 3, Thu = @ I + 4, Fri = @ I + 5, sat = @ I + 6 where Sun = 1
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 2
Begin
Update @ day2 set Tue = @ I + 1, wed = @ I + 2, Thu = @ I + 3, Fri = @ I + 4, sat = @ I + 5 where MON = 1
Set @ dtchk = @ dtchk + 5
End
Else if @ intchk = 3
Begin
Update @ day2 set wed = @ I + 1, Thu = @ I + 2, Fri = @ I + 3, sat = @ I + 4 where Tue = 1
Set @ dtchk = @ dtchk + 4
End
Else if @ intchk = 4
Begin
Update @ day2 set Thu = @ I + 1, Fri = @ I + 2, sat = @ I + 3 where wed = 1
Set @ dtchk = @ dtchk + 3
End
Else if @ intchk = 5
Begin
Update @ day2 set Fri = @ I + 1, sat = @ I + 2 where Thu = 1
Set @ dtchk = @ dtchk + 2
End
Else if @ intchk = 6
Begin
Update @ day2 set sat = @ I + 1 where Fri = 1
Set @ dtchk = @ dtchk + 1
End

Else if @ intchk = 7
Begin
Set @ dtchk = @ dtchk
End

insert into @ day2 (Sun) values (@ dtchk + 1)
set @ dtchk = @ dtchk + 1
If @ intchk = 1
begin
Update @ day2 set MON = @ I + 8, tue = @ I + 9, wed = @ I + 10, Thu = @ I + 11, Fri = @ I + 12, sat = @ I + 13 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 2
begin
Update @ day2 set MON = @ I + 7, tue = @ I + 8, wed = @ I + 9, Thu = @ I + 10, Fri = @ I + 11, sat = @ I + 12 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 3
begin
Update @ day2 set MON = @ I + 6, tue = @ I + 7, wed = @ I + 8, Thu = @ I + 9, Fri = @ I + 10, sat = @ I + 11 Where sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 4
begin
Update @ day2 set MON = @ I + 5, tue = @ I + 6, wed = @ I + 7, Thu = @ I + 8, Fri = @ I + 9, sat = @ I + 10 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 5
begin
Update @ day2 set MON = @ I + 4, tue = @ I + 5, wed = @ I + 6, Thu = @ I + 7, Fri = @ I + 8, sat = @ I + 9 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 6
begin
Update @ day2 set MON = @ I + 3, tue = @ I + 4, wed = @ I + 5, Thu = @ I + 6, Fri = @ I + 7, sat = @ I + 8 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end

Else if @ intchk = 7
Begin
Update @ day2 set MON = @ I + 2, Tue = @ I + 3, wed = @ I + 4, Thu = @ I + 5, Fri = @ I + 6, sat = @ I + 7 Where sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Insert into @ day2 (Sun) values (@ dtchk + 1)
Set @ dtchk = @ dtchk + 1
If @ intchk = 1
Begin
Update @ day2 set MON = @ I + 15, Tue = @ I + 16, wed = @ I + 17, Thu = @ I + 18, Fri = @ I + 19, sat = @ I + 20 Where sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 2
Begin
Update @ day2 set MON = @ I + 14, Tue = @ I + 15, wed = @ I + 16, Thu = @ I + 17, Fri = @ I + 18, sat = @ I + 19 Where sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 3
Begin
Update @ day2 set MON = @ I + 13, Tue = @ I + 14, wed = @ I + 15, Thu = @ I + 16, Fri = @ I + 17, sat = @ I + 18 where Sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 4
Begin
Update @ day2 set MON = @ I + 12, Tue = @ I + 13, wed = @ I + 14, Thu = @ I + 15, Fri = @ I + 16, sat = @ I + 17 where Sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 5
Begin
Update @ day2 set MON = @ I + 11, Tue = @ I + 12, wed = @ I + 13, Thu = @ I + 14, Fri = @ I + 15, sat = @ I + 16 Where sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Else if @ intchk = 6
Begin
Update @ day2 set MON = @ I + 10, Tue = @ I + 11, wed = @ I + 12, Thu = @ I + 13, Fri = @ I + 14, sat = @ I + 15 where Sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End

Else if @ intchk = 7
Begin
Update @ day2 set MON = @ I + 9, Tue = @ I + 10, wed = @ I + 11, Thu = @ I + 12, Fri = @ I + 13, sat = @ I + 14 Where sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
Insert into @ day2 (Sun) values (@ dtchk + 1)
Set @ dtchk = @ dtchk + 1

If @ intchk = 1
begin
Update @ day2 set MON = @ I + 22, Tue = @ I + 23, wed = @ I + 24, thu = @ I + 25, Fri = @ I + 26, sat = @ I + 27 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 2
begin
Update @ day2 set MON = @ I + 21, tue = @ I + 22, wed = @ I + 23, Thu = @ I + 24, Fri = @ I + 25, sat = @ I + 26 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 3
begin
Update @ day2 set MON = @ I + 20, tue = @ I + 21, wed = @ I + 22, Thu = @ I + 23, Fri = @ I + 24, sat = @ I + 25 Where sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 4
begin
Update @ day2 set MON = @ I + 19, tue = @ I + 20, wed = @ I + 21, Thu = @ I + 22, Fri = @ I + 23, sat = @ I + 24 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 5
begin
Update @ day2 set MON = @ I + 18, tue = @ I + 19, wed = @ I + 20, Thu = @ I + 21, Fri = @ I + 22, sat = @ I + 23 Where sun = @ dtchk
set @ dtchk = @ dtchk + 6
end
else if @ intchk = 6
begin
Update @ day2 set MON = @ I + 17, tue = @ I + 18, wed = @ I + 19, Thu = @ I + 20, Fri = @ I + 21, sat = @ I + 22 where Sun = @ dtchk
set @ dtchk = @ dtchk + 6
end

Else if @ intchk = 7
Begin
Update @ day2 set MON = @ I + 16, Tue = @ I + 17, wed = @ I + 18, Thu = @ I + 19, Fri = @ I + 20, sat = @ I + 21 where Sun = @ dtchk
Set @ dtchk = @ dtchk + 6
End
/* Set @ I = @ I + 1
End */

If @ dtchk <> @ dnum
Begin
Set @ dtchk = @ dtchk + 1
Insert into @ day2 (Sun) values (@ dtchk)
Set @ dtval = @ dtchk
Set @ dtchk = @ dtchk + 1
If @ dtchk <= @ dnum
Begin
Update @ day2 set MON = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
/* Insert into @ dd (jk1, JK2, jk3) values (@ dtchk, @ dtval, @ dnum )*/
If @ dtchk <= @ dnum
Begin
Update @ day2 set Tue = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
If @ dtchk <= @ dnum
Begin
Update @ day2 set wed = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
If @ dtchk <= @ dnum
Begin
Update @ day2 set Thu = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
If @ dtchk <= @ dnum
Begin
Update @ day2 set Fri = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
If @ dtchk <= @ dnum
Begin
Update @ day2 set sat = @ dtchk where Sun = @ dtval
Set @ dtchk = @ dtchk + 1
End
If @ dtchk <= @ dnum
Begin
Insert into @ day2 (Sun) values (@ dtchk)
Set @ dtval = @ dtchk
Set @ dtchk = @ dtchk + 1
If @ dtchk <= @ dnum
Begin
Update @ day2 set MON = @ dtchk where Sun = @ dtval
End
End
End
Insert into @ day1 select * From @ day2

Return
End
==============
Run: Select * From calander ("11", "2007 ")

After running the function, a calendar table is returned. How to Use it depends on your imagination.

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.