【问题标题】:write a query that will run all the days and the name of the day between two set dates [duplicate]编写一个查询,该查询将在两个设定日期之间运行所有日期和日期的名称[重复]
【发布时间】:2013-08-29 08:19:38
【问题描述】:

我正在尝试编写一个查询,该查询将在所有日期和两个设定日期之间运行的日期名称。

例子:

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

感谢任何帮助!

【问题讨论】:

  • 您使用的是哪个 DBMS?后格雷斯?甲骨文?

标签: sql


【解决方案1】:

您可以查看this fiddle

代码:

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

【讨论】:

    【解决方案2】:

    您可以使用该表值函数:

    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)
    

    【讨论】:

      【解决方案3】:

      你没有指定你的 DBMS,所以我假设 Postgres:

      select i::date, to_char(i, 'Day')
      from generate_series(timestamp '2005-12-28 00:00:00', 
                           timestamp '2006-12-30 00:00:00', interval '1' day) i;
      

      对此的 ANSI SQL 解决方案是:

      with recursive date_list (the_date) as (
          values ( date '2005-12-28' )
          union all
          select cast(p.the_date + interval '1' day as date)
          from date_list p
          where p.the_date <= date '2006-12-30
      )
      select * 
      from date_list;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 2011-08-22
        • 1970-01-01
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多