SQL Server 输出日历 (收集)

来源:互联网 发布:淘宝充值平台在哪里找 编辑:程序博客网 时间:2024/05/17 22:35

SQL Server 输出日历

--源文收集于网络

 

方法一:

 

create function fn_Calendar(@year int@month int)

returns nvarchar(max)

as

begin

    declare @result nvarchar(max), @Enter nvarchar(8)

    select @Enter = char(13)+char(10),  @result = ' Sun Mon The Wed Thu Fri Sta' + @Enter --表头

    declare @start datetime@end datetime

    select @start = rtrim(@year)+'-'+rtrim(@month)+'-1'@end = dateadd(mm, 1@start)    

    set @result = @result+replicate('    ', (datepart(dw, @start)+@@datefirst+6)%7)    --第一行前面的空格

    while datediff(d, @start@end)>0

    begin

        if (datepart(dw, @start)+@@datefirst)%7 = 1

            select @result = @result+@Enter --是否换行

        select @result = @result+right('   '+rtrim(day(@start)), 4), @start = dateadd(d, 1@start)

    end

    return @result

end

go

set datefirst 3 

print dbo.fn_Calendar(200712)

select dbo.fn_Calendar(200712)

set datefirst 7

 

drop function dbo.fn_Calendar

/**//*

 Sun Mon The Wed Thu Fri Sta

                           1

   2   3   4   5   6   7   8

   9  10  11  12  13  14  15

  16  17  18  19  20  21  22

  23  24  25  26  27  28  29

  30  31

------------------------------------------ Sun Mon The Wed Thu Fri Sta                           1

   2   3   4   5   6   7   8

   9  10  11  12  13  14  15

  16  17  18  19  20  21  22

  23  24  25  26  27  28  29

  30  31

(1 row(s) affected)

*/

 

 

方法二:

 

create function f_calendar(@year int,@month int)

returns @t table( varchar(4), varchar(4), varchar(4), varchar(4), varchar(4), varchar(4), varchar(4))

as

begin

 

    declare @a table(id int identity(0,1),date datetime)

 

    insert into @a(date) 

    select top 31 rtrim(@year)+'-'+rtrim(@month)+'-1' from sysobjects

 

    update @a set date=dateadd(dd,id,date)    

    insert into @t

    select

        max(case datepart(dw,date) when 7 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 1 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 2 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 3 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 4 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 5 then rtrim(day(date)) else '' end),

        max(case datepart(dw,date) when 6 then rtrim(day(date)) else '' end)

    from

        @a

    where

        month(date)=@month

    group by

        (case datepart(dw,date) when 7 then datepart(week,date)+1 else datepart(week,date) end)

 

    return

end

go

set datefirst 1

select * from dbo.f_calendar(2007,12)

 

/**//*

                           

---- ---- ---- ---- ---- ---- ---- 

                              1

2    3    4    5    6    7    8

9    10   11   12   13   14   15

16   17   18   19   20   21   22

23   24   25   26   27   28   29

30   31                  

*/

go

 

drop function f_calendar

go

 

 

方法三:

 

/*

功能:绘画日历

设计:OK_008

时间:2006-05

*/

DECLARE @Year nvarchar(4)

DECLARE @YearMonth nvarchar(7) --月份

DECLARE @strTop nvarchar(200)

DECLARE @ForI INT,@ForYear INT ,@MaxDay INT

DECLARE @RowX INT --行位置

DECLARE @strWeekDayList nvarchar(20)

DECLARE @strPrint nvarchar(300)

 

-- ======================================

SET @Year='2006' --请在这里输入年份

-- ======================================

SET @strTop= ''+char(9)+''+char(9)+'' +char(9)+''++char(9)+''++char(9)+''++char(9)+'' +char(13)+

    '───────────────────────────'

SET @strWeekDayList='日一二三四五六'

SET @ForYear=1

WHILE @ForYear <=12  --1月份至12月份

BEGIN

SET @YearMonth=@Year + '-' +CAST( @ForYear AS nvarchar(2))

SET @MaxDay=DAY(DATEADD(Day,-1,DATEADD(Month,1,@YearMonth+'-01')))

SET @RowX=CHARINDEX(RIGHT(DATENAME(WeekDay,@YearMonth+'-01'),1),@strWeekDayList)-1

SET @strPrint=''

SET @ForI=1

WHILE @ForI <=@RowX --构造1号的位置

    BEGIN

SET @strPrint=@strPrint+CHAR(9)

SET @ForI=@ForI+1

    END

SET @ForI=1

WHILE @ForI <=@MaxDay --构造2号到月底的位置

    BEGIN

SET @strPrint=@strPrint+CAST(@ForI AS nvarchar(2)) +Char(9)

SET @RowX=@RowX+1

SET @ForI=@ForI+1

IF (@RowX%7=0)

  BEGIN

SET @RowX=0

SET @strPrint=@strPrint+CHAR(13)

    END

  END

SET @ForYear=@ForYear+1

-- 打印输出一个月的结果

PRINT '━━━━━━━━━━━━━━━━━━━━━━━━━━━'

PRINT +Char(9)++Char(9)+'    '+@YearMonth+CHAR(10)

PRINT @strTop

PRINT @strPrint +CHAR(10)

  END

 

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-1

 

───────────────────────────

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-2

 

───────────────────────────

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-3

 

───────────────────────────

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-4

 

───────────────────────────

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-5

 

───────────────────────────

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-6

 

───────────────────────────

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-7

 

───────────────────────────

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-8

 

───────────────────────────

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-9

 

───────────────────────────

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-10

 

───────────────────────────

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-11

 

───────────────────────────

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

 

━━━━━━━━━━━━━━━━━━━━━━━━━━━

    2006-12

 

───────────────────────────

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

 

 

原创粉丝点击