選択した日付の間の月の開始日のみを取得したい
元 :
開始日 = 2012 -05-01 終了日 = 2013-04-01
私はこのようにしたい
日付 : 2012 -05-01 2012 -06-01 2012 -07-01 ..... 2013 -01-01 ..... 2012 -05-01
選択した日付の間の月の開始日のみを取得したい
元 :
開始日 = 2012 -05-01 終了日 = 2013-04-01
私はこのようにしたい
日付 : 2012 -05-01 2012 -06-01 2012 -07-01 ..... 2013 -01-01 ..... 2012 -05-01
CTEはトリックを行います
Declare @beginDt SmallDatetime = '13 Jan 2012'
Declare @endDt SmallDatetime = '2 Apr 2013';
With MonthList(aMon)
As
(Select DateAdd(month, datediff(month, 0, @beginDt), 31)
Union All
Select DateAdd(month, 1, aMon) From MonthList
Where DateAdd(month, 1, aMon) < @endDt)
Select aMon from MonthList
まず、カレンダー テーブルを作成します。次に、簡単なクエリを作成できます。
select [Date]
from dbo.Calendar
where IsFirstDayOfMonth = 1 and [Date] between '20120501' and '20130401'
このクエリは、関数ベースのソリューションを使用するよりもはるかに明確であり、カレンダー テーブルには他にも多くの用途があります。