0

すべての日と、設定された 2 つの日付の間の曜日の名前を実行するクエリを作成しようとしています。

例:

Date1 = 12/28/2005
Date2 = 12/30/2006

結果:

12/28/2005 Wednesday
12/29/2005 Thursday
12/30/2005 Friday
12/31/2005 Saturday
01/01/2006 Sunday
01/02/2006 Monday
01/03/2006 Tuesday

どんな助けでも大歓迎です!

4

3 に答える 3

0

このフィドルを確認できます。

コード:

DECLARE @Date1 DATETIME
DECLARE @Date2 DATETIME

SET @Date1 = '20051228'
SET @Date2 = '20061230'

;WITH cteSequence ( SeqNo) as
(
      SELECT 0
      UNION ALL
      SELECT SeqNo + 1
      FROM cteSequence
      WHERE SeqNo < DATEDIFF(d,@Date1,@Date2)
)
SELECT CONVERT(VARCHAR,DATEADD(d,SeqNo,@Date1),1) + ' ' + DATENAME(dw,DATEADD(d,SeqNo,@Date1))
FROM cteSequence
OPTION ( MAXRECURSION 0);


GO
于 2013-08-29T08:29:04.930 に答える
0

そのテーブル値関数を使用できます。

create function DateTable
(
  @FirstDate datetime,
  @LastDate datetime,
  @handle nvarchar(10)='day',
  @handleQuantity int=1
)
returns @datetable table (
  [date] datetime
)
AS
begin

  with CTE_DatesTable
  as 
  (
    select @FirstDate AS [date]
    union ALL
    select case @handle
        when 'month' then dateadd(month, @handleQuantity, [date])
        when 'year' then dateadd(year, @handleQuantity, [date])
        when 'hour' then dateadd(hour, @handleQuantity, [date])
        when 'minute' then dateadd(minute, @handleQuantity, [date])
        when 'second' then dateadd(second, @handleQuantity, [date])
        else dateadd(day, @handleQuantity, [date])
        end
    from CTE_DatesTable
    where @LastDate >=
    case @handle
        when 'month' then dateadd(month, @handleQuantity, [date])
        when 'year' then dateadd(year, @handleQuantity, [date])
        when 'hour' then dateadd(hour, @handleQuantity, [date])
        when 'minute' then dateadd(minute, @handleQuantity, [date])
        when 'second' then dateadd(second, @handleQuantity, [date])
        else DATEADD(day, @handleQuantity, [date])
        end
  )
  insert @datetable ([date])
  select [date] from CTE_DatesTable
  option (MAXRECURSION 0)

  return
end

次のように呼び出すことができます。

select [date],datepart(weekday,[date]) from dbo.DateTable('12/28/2005','12/30/2006',default,default)
于 2013-08-29T08:31:42.707 に答える