T1
起始年度
2008结果
假设今年是2010年,那结果就是
年度        标记
2011         1
2010         0
2009        -1
2008        -2
假设今年是2012年,那结果就是
年度        标记
2013         1
2012         0
2011        -1
2010        -2
2009        -3
2008        -4意思:就是取T1表中的年度到明年的年度值,标记的意思是 当前年度为0,当前年度以前的以负值递增,当前年度的下一年为1
谢谢

解决方案 »

  1.   

    update T1 set 标记=dateadd(year,getdate(),年度)
      

  2.   

    不好意思,上边错了,用这个
    [code=SQL]update T1 set 标记=datediff(year,getdate(),年度)code]
      

  3.   

    update T1 set 标记=datediff(year,getdate(),年度)
      

  4.   

    update T1 set 标记= 年度 - datepart(year,getdate())
      

  5.   

    这个不应该是个UPDATE语句啊,应该是一个SELECT语句
    T1表里只有一个字段,就是起始年度,不需要改变的上面说了,如果T1中的字段是2008
    假设今年是2010
    那我SELECT的结果就应该是
    年度     标记
    2011     1
    2010     0
    2009    -1
    2008    -2
      

  6.   

    Create Table T1(StartYear int)
    goinsert into T1 values(2008)
    go
    --------
    declare @CurYear as int
    select @CurYear =2010  --当前年度select number=number+2000,tag=(number+2000)-@CurYear
    from master..spt_values where Type='p'
    and (number+2000) between (Select top 1 StartYear from T1) and @CurYear+1
    order by tag desc/*number      tag         
    ----------- ----------- 
    2011        1
    2010        0
    2009        -1
    2008        -2(所影响的行数为 4 行)*/
      

  7.   


    --Ken Wong
    --测试数据
    declare @table table(ny char(4))
    insert into @table select '2008'
    --查询
    select convert(char(4),dateadd(year,a.number,b.ny),120) as 年度,
    datediff(year,'2010',convert(char(4),dateadd(year,a.number,b.ny),120)) as 标记
    from master..spt_values a,@table b where a.type='P'
    and convert(char(4),dateadd(year,a.number-1,b.ny),120)<='2010'
    order by convert(char(4),dateadd(year,a.number,b.ny),120) desc
    --结果
    ----------------
    年度    标记
    2011 1
    2010 0
    2009 -1
    2008 -2
    --改成存储过程。
      

  8.   


    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]create table [TB]([y] int)
    insert [TB]
    select 2008declare @i int,@j int,@s varchar(200)
    set @i=2010
    set @s=''
    set @j=@i-(select Y from TB)+2
    set @s='select * from (select top '+cast(@j as varchar(10))+' * from(
    select  年度=number+y,
    标记=number+y-'+cast(@i as varchar(10))+' from TB a,master..spt_values b where b.type=''p'')t
    order by 年度 asc)g order by  年度 desc'
    exec(@s)
    /*
    年度          标记          
    ----------- ----------- 
    2013        1
    2012        0
    2011        -1
    2010        -2
    2009        -3
    2008        -4*/
    drop table TB
      

  9.   

    declare @i int
    set @i=2010
    select * from (
    select  年度=number+y,
    标记=number+y-@i from TB a,master..spt_values b where b.type='p' and number<=@i-y+1)t
    order by 年度 desc
    /*
    年度          标记          
    ----------- ----------- 
    2011        1
    2010        0
    2009        -1
    2008        -2(所影响的行数为 4 行)*/