SQL SERVER中有YEAR和MONTH函数
取年用year(@t2),月用month(@t2)
然后转成字符串拼起来

解决方案 »

  1.   

    给你一过程,能满足需要吗?
    --取任意日期所对应的上下月或本月初日 Robin
    CREATE PROCEDURE [Pur_GetMonthForLDate] 
      @Date datetime, @GetDate varchar(20) output,@type int 
    ASif @type=-1  --取上月初日
    Set @GetDate=str(year(@Date-31),4)+'-'+Ltrim(str(month(@Date-31),2))+'-01'
    if @type=0  --取本月初日
    Set @GetDate=str(year(@Date),4)+'-'+ltrim(str(month(@Date),2))+'-01'
    if @type=1   --取下月初日
    Set @GetDate=str(year(@Date+31),4)+'-'+ltrim(str(month(@Date+31),2))+'-01'
    --------------------------------------------------------------------------------------------------------------------------
    ----------------------------------调用方法
    ---        declare @getdate varchar(20),@CurrDate datetime
    ---        set @currdate='2002-6-5'
    ---        exec Pur_GetMonthForLDate @currdate,@getdate output,-1
    ---        set @Currdate=@getdate
    ---        select @Currdate
    --------------------------------------------------------------------------------------------------------------------------
    GO
      

  2.   

    本月最后一天
    select dateadd(day,-1,cast(datepart(year,getdate()) as varchar(4)) 
    +'-' +cast(datepart(month,getdate()) as varchar(2)) + '-' + '1')楼上的下月初日应该有问题,好像没考虑各种月份吧。
      

  3.   

    同意 newly_ignorant(不学无术)
      

  4.   

    第一天:
        select convert(char(10),dateadd(d,-datepart(d,getdate())+1,getdate()),120)
      

  5.   

    最后一天:
       select convert(char(10),dateadd(d,-datepart(d,getdate()),dateadd(m,1,getdate())),120)
      

  6.   

    上面的getdate()换成你的@t2,就行了