给你一过程,能满足需要吗? --取任意日期所对应的上下月或本月初日 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
本月最后一天 select dateadd(day,-1,cast(datepart(year,getdate()) as varchar(4)) +'-' +cast(datepart(month,getdate()) as varchar(2)) + '-' + '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
select dateadd(day,-1,cast(datepart(year,getdate()) as varchar(4))
+'-' +cast(datepart(month,getdate()) as varchar(2)) + '-' + '1')楼上的下月初日应该有问题,好像没考虑各种月份吧。
select convert(char(10),dateadd(d,-datepart(d,getdate())+1,getdate()),120)
select convert(char(10),dateadd(d,-datepart(d,getdate()),dateadd(m,1,getdate())),120)