我在以下情況下處理日期操作。查找在給定範圍內的日期
輸入:DateA,DateB,MONTH_COUNT
前提條件:DateB總是比DateA
Output:
1)I need to find newstartdate and newEndDate from DateB
newStartDate<DateA<newEndDate.
2)For newstartDate ,dateB is subtracted by month count .
3)NewEndDate is newStartDate+month_count.
4)Repeat 2,3 until the condition 1 becomes true.
我想出了while條件進行this.Is有什麼辦法可以更大簡化這個邏輯?
Declare @dateA datetime
Declare @dateB Datetime
declare @month_count int
declare @newstartdate datetime
declare @newenddate datetime
set @dateA= '2013-02-21'
set @dateB='2013-08-23'
set @month_count=3
SET @newstartdate=DATEADD(mm,[email protected]_count,@dateB)
Set @newenddate=DATEADD(mm,@month_count ,@newstartdate)
WHILE Not (@newstartdate < @dateA AND @dateA<@newenddate)
BEGIN
SET @newstartdate=DATEADD(mm,[email protected]_count,@newstartdate)
Set @newenddate=DATEADD(mm,@month_count,@newstartdate)
Select @dateA 'DateA',@newstartdate '@newstartdate',@newenddate '@newenddate'
END
一個完整側面說明,我會建議總是寫日期作爲格式'YYYY-MM-DD'字符串因爲這將消除任何文化問題 – freefaller
感謝.Modified相同 – Krishna