見DATEADD()
select convert(datetime,'6/1/2010')
select DATEADD(day,-1,DATEADD(month,1,convert(datetime,'6/1/2010'))) --6/30/2010
select DATEADD(month,1,convert(datetime,'6/1/2010')) --7/1/2010
你可能會在一個存儲過程,它會生成一個臨時表,或快速和真髒只是一個大的查詢運行這個...
我毫不猶豫地只爲你做這件事,但什麼乾草...
create procedure getFiscalYear
@startDate nvarchar(10)
as begin
declare @myDate datetime
set @myDate = convert(datetime,@startDate)
select 1 as Period, @myDate as startDate, DATEADD(day,-1,DATEADD(month,1,@myDate)) as endDate
union
select 2 as Period, DATEADD(month,1,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,2,@myDate)) as endDate
union
select 3 as Period, DATEADD(month,2,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,3,@myDate)) as endDate
union
select 4 as Period, DATEADD(month,3,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,4,@myDate)) as endDate
union
select 5 as Period, DATEADD(month,4,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,5,@myDate)) as endDate
union
select 6 as Period, DATEADD(month,5,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,6,@myDate)) as endDate
union
select 7 as Period, DATEADD(month,6,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,7,@myDate)) as endDate
union
select 8 as Period, DATEADD(month,7,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,8,@myDate)) as endDate
union
select 9 as Period, DATEADD(month,8,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,9,@myDate)) as endDate
union
select 10 as Period, DATEADD(month,9,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,10,@myDate)) as endDate
union
select 11 as Period, DATEADD(month,10,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,11,@myDate)) as endDate
union
select 12 as Period, DATEADD(month,11,@myDate) as startDate, DATEADD(day,-1,DATEADD(month,12,@myDate)) as endDate
end
這是醜陋的方式...
鮑勃謝謝你的查詢其工作,但我怎麼才能得到只有日期部分?實際上它帶着時間戳來。對不起,要求太多我有一個oracle背景,並且是SQLSERVER的新手。 – user342944 2010-08-20 12:35:00
哦BOB我已經通過Google搜索... 轉換(varchar(10),DATEADD(month,number - 1,@StartDate),105) thankssssssssssssssssssssssssssss – user342944 2010-08-20 12:39:55