代码之家  ›  专栏  ›  技术社区  ›  chrisSpaceman

开始日期和结束日期之间的一系列季度日期

  •  0
  • chrisSpaceman  · 技术社区  · 8 年前

    declare @startDate datetime
    declare @endDate datetime
    
    set @startDate= '01-jan-2014'
    set @endDate= '01-jul-2017'
    
    ;With cte
    As
    ( Select @startDate date1
    Union All
    Select DateAdd(Month,3,date1)   From cte where date1 < @endDate 
    ) select cast(cast( Year(date1)*10000 + MONTH(date1)*100 + 1 as 
    varchar(255)) as date) quarterlyDates From cte
    

    这将产生:

    quarterlyDates
    --------------
    2014-01-01
    2014-04-01
    2014-07-01
    2014-10-01 ...
    

    我想将cte的输出连接成一个字符串,如下所示:

    "'01-jan-2014', '01-apr-2014, '01-jul-2014'..." 
    

    3 回复  |  直到 8 年前
        1
  •  1
  •   S3S    8 年前

    不知道你为什么想…但只要把底部的cte包起来,然后用一些东西。

    declare @table table(quarterlyDates date)
    insert into @table
    values
    ('2014-01-01'),
    ('2014-04-01'),
    ('2014-07-01'),
    ('2014-10-01')
    
    SELECT stuff((
        SELECT ', ' + cast(quarterlyDates as varchar(max))
        FROM @table
        FOR XML PATH('')
        ), 1, 2, '')
    

    在你的代码中…虽然第二个CTE不是必需的,但为了清晰起见,我把它留下了。

    declare @startDate datetime
    declare @endDate datetime
    
    set @startDate= '01-jan-2014'
    set @endDate= '01-jul-2017'
    
    ;With cte
    As
    ( Select @startDate date1
    Union All
    Select DateAdd(Month,3,date1)   From cte where date1 < @endDate 
    ), 
    
    cte2 as(
    select cast(cast( Year(date1)*10000 + MONTH(date1)*100 + 1 as 
    varchar(255)) as date) quarterlyDates From cte)
    
    SELECT stuff((
        SELECT ', ' + cast(quarterlyDates as varchar(max))
        FROM cte2
        FOR XML PATH('')
        ), 1, 2, '');
    
        2
  •  1
  •   Pரதீப்    8 年前

    使用 FOR XML Path type

    ;WITH cte 
         AS (SELECT @startDate date1 
             UNION ALL 
             SELECT Dateadd(month, 3, date1) 
             FROM   cte 
             WHERE  date1 < @endDate) 
    SELECT Stuff((SELECT ',' + CONVERT(VARCHAR(15), date1, 106) quarterlyDates 
                  FROM   cte 
                  FOR xml path, type).value('.[1]', 'nvarchar(max)'), 1, 1, ''); 
    

    注: 我在决赛中改变了不必要的操作 select 106 在里面 Convert 函数以获取所需的输出格式

        3
  •  1
  •   John Cappelletti    8 年前

    declare @startDate datetime = '01-jan-2014'
    declare @endDate datetime   = '01-jul-2017'
    
    ;With cte
    As
    ( Select date1 = @startDate
            ,dates = ''''+convert(varchar(max),convert(varchar(11),@startDate,113))+''''
      Union All
      Select DateAdd(Month,3,date1)   
            ,cte.dates+','''+convert(varchar(11),DateAdd(Month,3,date1)   ,106)+''''
       From cte where date1 < @endDate 
    ) 
    Select Top 1 with ties 
           Dates=lower(replace(Dates,' ','-') )
     From cte
     Order By Date1 Desc
    

    退换商品

    '01-jan-2014','01-apr-2014','01-jul-2014','01-oct-2014','01-jan-2015','01-apr-2015','01-jul-2015','01-oct-2015','01-jan-2016','01-apr-2016','01-jul-2016','01-oct-2016','01-jan-2017','01-apr-2017','01-jul-2017'