表是这样的:
  月份     人员编号  补贴金额    补贴有效期    卡有效期
2011-12      001        30       2011-12-31    2015-01-01
2011-12      002        30       2011-12-31    2015-01-01
2011-12      003        30       2011-12-31    2015-01-01
这是12月份的
我现在想通过一条SQL语句能够让表的‘月分’与‘补贴有效期’能够随变而变化。
比如:现在又过了一个月1月的表的显示就变成这样
  月份     人员编号  补贴金额    补贴有效期    卡有效期
2012-1      001        30       2012-1-31    2015-01-01
2012-1      002        30       2012-1-31    2015-01-01
2012-1      003        30       2012-1-31    2015-01-01
又过一个月到了2月表的显示成这样
  月份     人员编号  补贴金额    补贴有效期    卡有效期
2012-2      001        30       2012-2-28    2015-01-01
2012-2      002        30       2012-2-28    2015-01-01
2012-2      003        30       2012-2-28    2015-01-01
意思就是这样,如何用SQL语句能够完成

解决方案 »

  1.   

    [月份]=CONVERT(VARCHAR(7),DATEADD(m,1,[月份]+'-01' ),120)
      

  2.   

    如果是去当前日期,就用getdate
      

  3.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([月份] nvarchar(7),[人员编号] nvarchar(3),[补贴金额] int,[补贴有效期] Datetime,[卡有效期] Datetime)
    Insert #T
    select N'2011-12',N'001',30,'2011-12-31','2015-01-01' union all
    select N'2011-12',N'002',30,'2011-12-31','2015-01-01' union all
    select N'2011-12',N'003',30,'2011-12-31','2015-01-01'
    Go
    Select [月份]=REPLACE(CONVERT(VARCHAR(7),DATEADD(m,1,[月份]+'-01' ),120),'-0','-'),[人员编号],[补贴金额],[补贴有效期],[卡有效期]
    from #T
    /*
    月份 人员编号 补贴金额 补贴有效期 卡有效期
    2012-1 001 30 2011-12-31 00:00:00.000 2015-01-01 00:00:00.000
    2012-1 002 30 2011-12-31 00:00:00.000 2015-01-01 00:00:00.000
    2012-1 003 30 2011-12-31 00:00:00.000 2015-01-01 00:00:00.000
    */
      

  4.   

    3#您的补贴有效期还是没有更新过来我的原代码是这样,您指教下
    eclare @days int,@datebeg datetime,@dateend datetime,@month varchar(100),
            @carddate datetime
    set @datebeg =convert(datetime, left(:补贴月份 ,4)
                  +'-'+right(:补贴月份 ,2)+'-01' ,120)
    set @dateend =dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,@datebeg)+1, 0)) 
    set @days =datepart(day,@dateend)
    set @month =left(:补贴月份,4)+'-'+right(:补贴月份 ,2)
    set @carddate='2015-01-01'
    select A0188,sum(floor(K2010)) k2010 into #subsidies
     from k20
     where dateadd(mm,1,k2006) between @datebeg and @dateend 
       and dateadd(mm,1,k2007) between @datebeg and @dateend
     group by A0188 
    select @month '月份',
           A01.A0190 '人员编号',
           convert(int,A01.A01B9-isnull(k2010,0)) '补贴金额',
           convert(varchar(10),@dateend,120) '补贴有效期',
           convert(varchar(10),@carddate,120) '卡有效期'
    from A01 left join #subsidieson A01.a0188=#subsidies
    .a0188  
    where A01.A01B9 is not null and  A01.A0191 = '促销人员'
    and  convert(int,A01.A01B9-isnull(k2010,0))>0
    您主要看时间这一块,帮忙解决下,谢谢
      

  5.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([月份] nvarchar(7),[人员编号] nvarchar(3),[补贴金额] int,[补贴有效期] Datetime,[卡有效期] Datetime)
    Insert #T
    select N'2011-12',N'001',30,'2011-12-31','2015-01-01' union all
    select N'2011-12',N'002',30,'2011-12-31','2015-01-01' union all
    select N'2011-12',N'003',30,'2011-12-31','2015-01-01'
    Go
    Select 
    [月份]=REPLACE(CONVERT(VARCHAR(7),DATEADD(m,1,[月份]+'-01' ),120),'-0','-'),[人员编号],[补贴金额],
    [补贴有效期]=DATEADD(m,2,[补贴有效期]-DAY([补贴有效期])+1)-1,
    [卡有效期]
    from #T
    /*
    月份 人员编号 补贴金额 补贴有效期 卡有效期
    2012-1 001 30 2012-01-31 00:00:00.000 2015-01-01 00:00:00.000
    2012-1 002 30 2012-01-31 00:00:00.000 2015-01-01 00:00:00.000
    2012-1 003 30 2012-01-31 00:00:00.000 2015-01-01 00:00:00.000
    */
      

  6.   

    select *,left((convert(varchar(10),(dateadd(m,1,(convert(varchar(12),月份)+'-01'))),120)),7),dateadd(m,1,补贴有效期) from #t