Declare @FromDateString varchar(12) --查询的起始时间
Delcare @ThruDateString varchar(12) --截止时间
上周的第一天至上周的最后一天;(我是以Monday为开始 Sunday为结束)
SET DATEFIRST 1
Set @FromDateString = convert(varchar,dateadd(day,-1*datepart(weekday,getdate())-6,getdate()),101)
Set @ThruDateString = convert(varchar,dateadd(day,-1*datepart(weekday,getdate()),getdate()),101)
本月的第一天至本月最后一天;
Set @FromDateString = convert(varchar,Month(getdate()))+'/'+'01/'+convert(varchar,year(getdate()))
Set @FromDateString = convert(varchar,dateadd(day,-1,convert(varchar,Month(dateadd(month,1,getdate())))+'/'+'01/'+convert(varchar,year(dateadd(month,1,getdate())))),101)
季度的第一天至季度最后一天;
select @FromDateString = case when Month(getdate()) in (1,2,3) then '01/01/'+convert(char,year(getdate())
when Month(getdate()) in (4,5,6) then '04/01/'+convert(char,year(getdate())
when Month(getdate()) in (7,8,9) then '07/01/'+convert(char,year(getdate())
else '10/01/'+convert(char,year(getdate()) end
select @ThruDateString = case when Month(getdate()) in (1,2,3) then '03/31/'+convert(char,year(getdate())
when Month(getdate()) in (4,5,6) then '06/30/'+convert(char,year(getdate())
when Month(getdate()) in (7,8,9) then '09/30/'+convert(char,year(getdate())
else '12/31/'+convert(char,year(getdate()) end
上月20日至本月19日 :
set @FromDateString = left(convert(varchar,dateadd(month,-1,getdate()),101),3)+'20'+right(convert(varchar,dateadd(month,-1,getdate()),101),5)
set @ThruDateString = left(convert(varchar,getdate(),101),3)+'19'+right(convert(varchar,getdate(),101),5)