select year(getdate()+7),month(getdate()+7),day(getdate()+7)
Union all
select year(getdate()+8),month(getdate()+8),day(getdate()+8)
Union all
select year(getdate()+9),month(getdate()+9),day(getdate()+9)select year(getdate()+7),month(getdate()+7),day(getdate()+7)
Union all
select year(getdate()+8),month(getdate()+8),day(getdate()+8)
Union all
select year(getdate()+9),month(getdate()+9),day(getdate()+9)                                    
----------- ----------- ----------- 
2004        1           7
2004        1           8
2004        1           9(所影响的行数为 3 行)

解决方案 »

  1.   

    select year(getdate()+7),month(getdate()+7),day(getdate()+7)
    Union all
    select year(getdate()+8),month(getdate()+8),day(getdate()+8)
    Union all
    select year(getdate()+9),month(getdate()+9),day(getdate()+9)                                    
    ----------- ----------- ----------- 
    2004        1           7
    2004        1           8
    2004        1           9(所影响的行数为 3 行)
      

  2.   

    select top 1500 identity(int,1,1) as id into # from syscolumns a ,syscolumns bselect getdate()+id
    from # where id>=7 and id<=datediff(dy,getdate(),'2005-12-31')
      

  3.   

    select top 1500 identity(int,1,1) as id into # from syscolumns a ,syscolumns bselect year(getdate()+id) yy,month(getdate()+id) mm,day(getdate()+id) dd
    from # where id>=7 and id<=datediff(dy,getdate(),'2005-12-31')
    yy          mm          dd          
    ----------- ----------- ----------- 
    2004        1           7
    2004        1           8
    2004        1           9
    2004        1           10
    .....
    2005        12          28
    2005        12          29
    2005        12          30
    2005        12          31(所影响的行数为 725 行)
      

  4.   

    select year(getdate()) as nyear,month(getdate()) as nmonth, day(getdate()+7) as nday
    union all
    select year(getdate()) as nyear,month(getdate()) as nmonth, day(getdate()+8) as nday
    union all
    select year(getdate()) as nyear,month(getdate()) as nmonth, day(getdate()+9) as nday
      

  5.   

    这样也可
    select datepart(year,getdate()),datepart(month,getdate()),datepart(day,getdate(+7)
    union all
    select datepart(year,getdate()),datepart(month,getdate()),datepart(day,getdate(+8)
    union all
    select datepart(year,getdate()),datepart(month,getdate()),datepart(day,getdate(+9)
      

  6.   

    --建立函数
    create function dbo.f_getid()
    returns int
    as
    begin
    declare @id int
    select @id=max(tmpid) from table1
    set @id=isnull(@id,0)+1
    return(@id)
    end
    go
    --建立表
    create table table1 (
    MyYear varchar(100) default year(dateadd(day,dbo.f_getid(),getdate()))
    ,MyMonth varchar(100)   default month(dateadd(day,dbo.f_getid(),getdate())),
    MyDay varchar(100) default day(dateadd(day,dbo.f_getid(),getdate())) ,tmpid int)
    --测试插入值
    declare @j int 
    set @j=7
    while @j<datediff(day, getdate(),'2005-2-2')+7 begin 
    insert into table1 (tmpid) values (@j)
    set @j=@j+1
    end
    select * from table1
    --删除测试环境
    drop table table1
     drop function f_getid
      

  7.   

    select top 1500 identity(int,7,1) as id into # from syscolumns a ,syscolumns bselect id, year(dateadd(day,id,getdate()) )年,month(dateadd(day,id,getdate())) 月,
    day(dateadd(day,id,getdate())) 日
    from # where  id<=datediff(day,getdate(),'2005-12-31')
    drop table #所影响的行数为 1500 行)id          年           月           日           
    ----------- ----------- ----------- ----------- 
    7           2004        1           7
    8           2004        1           8
    9           2004        1           9
    10          2004        1           10
    11          2004        1           11
    12          2004        1           12
    13          2004        1           13
    14          2004        1           14
    15          2004        1           15
    16          2004        1           16
    17          2004        1           17
      

  8.   

    create view sss  as
    select year(getdate()+id) yy,month(getdate()+id) mm,day(getdate()+id) dd
    from # where id>=7 and id<=datediff(dy,getdate(),'2005-12-31')
    不行啊:
    服务器: 消息 4508,级别 16,状态 1,过程 sss,行 3
    不允许在临时表上建立视图或函数。以 '#' 开头的表名表示临时表。
      

  9.   

    在你的存储过程过函数里判断,
    MSSQL MONTH(), DAY(), YEAR()作用后都是省0的.
      

  10.   

    谢谢各位了,但还有一个问题请教:当显示1月1日时,无法显示为01月01日,怎么做才能让月、日保持2位数???drop table temptableselect top 1500 identity(int,1,1) as nid into temptable from syscolumns a ,syscolumns bselect year(getdate()+nid) yy,
    right(('0'+cast(month(getdate()+nid) as nvarchar)),2) mm,
    right(('0'+cast(day(getdate()+nid) as nvarchar)),2) dd
    from temptable where nid>=7 and nid<=datediff(dy,getdate(),'2005-12-31')------------------------------------------
    (所影响的行数为 1500 行)
    (所影响的行数为 725 行)
      

  11.   

    select CONVERT(varchar(10), getDate(),121) --不要时间2002-01-01
    select CONVERT(varchar(10), getDate(),120) --不要时间2002-1-1
    select convert(char(8),getdate(),112) ----20020101
    select convert(char(8),getdate(),108)  ---06:05:05 101 美国 mm/dd/yyyy 
    2 102 ANSI yy.mm.dd 
    3 103 英国/法国 dd/mm/yy 
    4 104 德国 dd.mm.yy 
    5 105 意大利 dd-mm-yy 
    6 106 - dd mon yy 
    7 107 - mon dd, yy 
    8 108 - hh:mm:ss 
    - 9 或 109 (*)  默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM) 
    10 110 美国 mm-dd-yy 
    11 111 日本 yy/mm/dd 
    12 112 ISO yymmdd 
    - 13 或 113 (*)  欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h) 
    14 114 - hh:mi:ss:mmm(24h) 
    - 20 或 120 (*)  ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff] 
    - 21 或 121 (*)  ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff] 
    - 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格) 
    - 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM 
    - 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM