建立一个存储过程计算出一年有几周,每一周是几年几月几号到几年几月几号数据库是SQL2005弄了很久 弄不出啊 没头绪  求指教

解决方案 »

  1.   

    select datepart(weekday,dateadd(dd,number,'2011-01-01'))-1 as 星期,dateadd(dd,number,'2011-01-01') as 日期
    from master..spt_values
    where type='p' and number < datediff(dd,'2011-01-01','2012-01-01') 
      

  2.   

    做一个日历表
    http://topic.csdn.net/u/20100528/16/f3c160a2-6d97-4e19-8f74-154d34a940d7.html?41962
      

  3.   


    with cte as
    (
    select dt=dateadd(d,number,'2011-01-01') 
    from master..spt_values 
    where type = 'P' and dateadd(d,number,'2011-01-01') <= '2011-12-31'
    )
    select 周 = datepart(wk,dt), 开始日期 = min(dt), 结束日期 = max(dt) from cte group by datepart(wk,dt)/*
    周           开始日期                    结束日期
    ----------- ----------------------- -----------------------
    1           2011-01-01 00:00:00.000 2011-01-01 00:00:00.000
    2           2011-01-02 00:00:00.000 2011-01-08 00:00:00.000
    3           2011-01-09 00:00:00.000 2011-01-15 00:00:00.000
    4           2011-01-16 00:00:00.000 2011-01-22 00:00:00.000
    5           2011-01-23 00:00:00.000 2011-01-29 00:00:00.000
    6           2011-01-30 00:00:00.000 2011-02-05 00:00:00.000
    7           2011-02-06 00:00:00.000 2011-02-12 00:00:00.000
    8           2011-02-13 00:00:00.000 2011-02-19 00:00:00.000
    9           2011-02-20 00:00:00.000 2011-02-26 00:00:00.000
    10          2011-02-27 00:00:00.000 2011-03-05 00:00:00.000
    11          2011-03-06 00:00:00.000 2011-03-12 00:00:00.000
    12          2011-03-13 00:00:00.000 2011-03-19 00:00:00.000
    13          2011-03-20 00:00:00.000 2011-03-26 00:00:00.000
    14          2011-03-27 00:00:00.000 2011-04-02 00:00:00.000
    15          2011-04-03 00:00:00.000 2011-04-09 00:00:00.000
    16          2011-04-10 00:00:00.000 2011-04-16 00:00:00.000
    17          2011-04-17 00:00:00.000 2011-04-23 00:00:00.000
    18          2011-04-24 00:00:00.000 2011-04-30 00:00:00.000
    19          2011-05-01 00:00:00.000 2011-05-07 00:00:00.000
    20          2011-05-08 00:00:00.000 2011-05-14 00:00:00.000
    21          2011-05-15 00:00:00.000 2011-05-21 00:00:00.000
    22          2011-05-22 00:00:00.000 2011-05-28 00:00:00.000
    23          2011-05-29 00:00:00.000 2011-06-04 00:00:00.000
    24          2011-06-05 00:00:00.000 2011-06-11 00:00:00.000
    25          2011-06-12 00:00:00.000 2011-06-18 00:00:00.000
    26          2011-06-19 00:00:00.000 2011-06-25 00:00:00.000
    27          2011-06-26 00:00:00.000 2011-07-02 00:00:00.000
    28          2011-07-03 00:00:00.000 2011-07-09 00:00:00.000
    29          2011-07-10 00:00:00.000 2011-07-16 00:00:00.000
    30          2011-07-17 00:00:00.000 2011-07-23 00:00:00.000
    31          2011-07-24 00:00:00.000 2011-07-30 00:00:00.000
    32          2011-07-31 00:00:00.000 2011-08-06 00:00:00.000
    33          2011-08-07 00:00:00.000 2011-08-13 00:00:00.000
    34          2011-08-14 00:00:00.000 2011-08-20 00:00:00.000
    35          2011-08-21 00:00:00.000 2011-08-27 00:00:00.000
    36          2011-08-28 00:00:00.000 2011-09-03 00:00:00.000
    37          2011-09-04 00:00:00.000 2011-09-10 00:00:00.000
    38          2011-09-11 00:00:00.000 2011-09-17 00:00:00.000
    39          2011-09-18 00:00:00.000 2011-09-24 00:00:00.000
    40          2011-09-25 00:00:00.000 2011-10-01 00:00:00.000
    41          2011-10-02 00:00:00.000 2011-10-08 00:00:00.000
    42          2011-10-09 00:00:00.000 2011-10-15 00:00:00.000
    43          2011-10-16 00:00:00.000 2011-10-22 00:00:00.000
    44          2011-10-23 00:00:00.000 2011-10-29 00:00:00.000
    45          2011-10-30 00:00:00.000 2011-11-05 00:00:00.000
    46          2011-11-06 00:00:00.000 2011-11-12 00:00:00.000
    47          2011-11-13 00:00:00.000 2011-11-19 00:00:00.000
    48          2011-11-20 00:00:00.000 2011-11-26 00:00:00.000
    49          2011-11-27 00:00:00.000 2011-12-03 00:00:00.000
    50          2011-12-04 00:00:00.000 2011-12-10 00:00:00.000
    51          2011-12-11 00:00:00.000 2011-12-17 00:00:00.000
    52          2011-12-18 00:00:00.000 2011-12-24 00:00:00.000
    53          2011-12-25 00:00:00.000 2011-12-31 00:00:00.000(53 行受影响)
    */
      

  4.   


    select  substring(convert(varchar,dateadd(day,x,col),120),1,10) as 开始日期,
    substring(convert(varchar,dateadd(day,x,col+6),120),1,10) as 结束日期 from 

    select cast('2011-1-1' as datetime) as col 
    )a cross join 

    select  top 365 b8.i+b7.i + b6.i + b5.i + b4.i +b3.i +b2.i + b1.i + b0.i x 
    from(select 0 i union all select 1) b0 
    cross join(select 0 i union all select 2) b1 
    cross join(select 0 i union all select 4) b2 
    cross join(select 0 i union all select 8) b3 
    cross join(select 0 i union all select 16) b4 
    cross join(select 0 i union all select 32) b5 
    cross join(select 0 i union all select 64) b6 
    cross join(select 0 i union all select 128) b7 
    cross join(select 0 i union all select 256) b8 
    order by 1
    )b 
    where datepart(dw,dateadd(day,x,col))=2
    /*
    开始日期                 结束日期
    -------------------- --------------------
    2011-01-03           2011-01-09
    2011-01-10           2011-01-16
    2011-01-17           2011-01-23
    2011-01-24           2011-01-30
    2011-01-31           2011-02-06
    2011-02-07           2011-02-13
    2011-02-14           2011-02-20
    2011-02-21           2011-02-27
    2011-02-28           2011-03-06
    2011-03-07           2011-03-13
    2011-03-14           2011-03-20
    2011-03-21           2011-03-27
    2011-03-28           2011-04-03
    2011-04-04           2011-04-10
    2011-04-11           2011-04-17
    2011-04-18           2011-04-24
    2011-04-25           2011-05-01
    2011-05-02           2011-05-08
    2011-05-09           2011-05-15
    2011-05-16           2011-05-22
    2011-05-23           2011-05-29
    2011-05-30           2011-06-05
    2011-06-06           2011-06-12
    2011-06-13           2011-06-19
    2011-06-20           2011-06-26
    2011-06-27           2011-07-03
    2011-07-04           2011-07-10
    2011-07-11           2011-07-17
    2011-07-18           2011-07-24
    2011-07-25           2011-07-31
    2011-08-01           2011-08-07
    2011-08-08           2011-08-14
    2011-08-15           2011-08-21
    2011-08-22           2011-08-28
    2011-08-29           2011-09-04
    2011-09-05           2011-09-11
    2011-09-12           2011-09-18
    2011-09-19           2011-09-25
    2011-09-26           2011-10-02
    2011-10-03           2011-10-09
    2011-10-10           2011-10-16
    2011-10-17           2011-10-23
    2011-10-24           2011-10-30
    2011-10-31           2011-11-06
    2011-11-07           2011-11-13
    2011-11-14           2011-11-20
    2011-11-21           2011-11-27
    2011-11-28           2011-12-04
    2011-12-05           2011-12-11
    2011-12-12           2011-12-18
    2011-12-19           2011-12-25
    2011-12-26           2012-01-01
    */