格式化此日期范围的输出会查询结果

时间:2014-07-08 14:01:31

标签: sql-server

下面的查询显示了给定日期(Time_Stamp列中最近一个季度)的下一个季度的第一天和最后一天。我试图将输出结构更改为显示为第二个查询。

专栏Time_Stamp

Time_Stamp
-----------
2014-06-04 16:01:14.000
2014-06-04 15:55:33.000
2014-06-04 15:45:05.000

新查询

SELECT 
    --first day of next quarter
    DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 1, 0) as StartDate,
    --last day of next quarter
    DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +2, 0)) as EndDate,
    --first day 2 quarters ahead
    DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 2, 0) as StartDate,
    --last day, 2 quarters ahead
    DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +3, 0)) as EndDate,
    --first day 3 quarters ahead
    DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 3, 0) as StartDate,
    --last day 3 quarters ahead
    DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +4, 0)) as EndDate
FROM Survey
WHERE MainHospital = 'Hospital1'

输出

StartDate               EndDate                 StartDate               EndDate                 StartDate               EndDate
----------------------- ----------------------- ----------------------- ----------------------- ----------------------- -----------------------
2014-07-01 00:00:00.000 2014-09-30 00:00:00.000 2014-10-01 00:00:00.000 2014-12-31 00:00:00.000 2015-01-01 00:00:00.000 2015-03-31 00:00:00.000

第二个查询,它提供错误的日期,但首选格式:

 WITH Start AS
 (
    SELECT 
        -- Returns first day of next quarter
        DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 1, 0) as StartDate,
        -- Returns last day of next quarter
        DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +2, 0)) as EndDate
    FROM Survey
    WHERE MainHospital = 'Hospital1'    
),
Results AS
(
   SELECT StartDate, EndDate from Start
   UNION
   SELECT DATEADD(DAY, 1, EndDate), DATEADD(day, 91, EndDate) FROM Start
   UNION
   SELECT DATEADD(DAY, 92, EndDate), DATEADD(day, 182, EndDate) FROM Start
)
SELECT LEFT(StartDate,11) AS StartDate, LEFT(EndDate,11) AS EndDate FROM Results`

首选输出格式:

 StartDate   EndDate
----------- -----------
Jul  1 2014 Sep 30 2014
Oct  1 2014 Dec 30 2014
Dec 31 2014 Mar 31 2015

3 个答案:

答案 0 :(得分:1)

您可能需要调整一下以适合您的代码,但这样可以获得您所需的日期格式:

 SELECT CONVERT(VARCHAR(12), StartDate, 107) AS StartDate

我使用它作为日期时间的参考,它非常有用: http://www.sql-server-helper.com/tips/date-formats.aspx

答案 1 :(得分:1)

DECLARE @TimeTable TABLE
( Time_Stamp DATETIME )

INSERT INTO @TimeTable
( Time_Stamp )
VALUES
('2011-06-04 16:01:14.000'),
('2014-06-04 16:01:14.000'),
('2014-06-04 15:55:33.000'),
('2014-06-04 15:45:05.000');    

SELECT CONVERT(VARCHAR(12),DATEADD(QUARTER, n.number + 1, t.BeginYear)) as StartDate,
    CONVERT(VARCHAR(12),DATEADD(SECOND, - 1, DATEADD(QUARTER, n.number + 2, t.BeginYear))) as EndDate
FROM
(
    select top 400 row_number() over(order by t1.number) as number
    from master..spt_values t1 
    cross join master..spt_values t2
) n
OUTER APPLY
(
    SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, MIN(t.Time_Stamp)), 0) BeginYear, MAX(t.Time_Stamp) MaxDate FROM @TimeTable t
) t
WHERE DATEADD(QUARTER, n.number, t.BeginYear) < DATEADD(QUARTER, +2, t.MaxDate)

我创建了一个数字表格,用于生成业务规则中所需的最后一个日期之后的第一个日期到3个季度的所有季度值,然后根据您的数据应用以显示开始日期和结束日期。

我在2011年添加了一个奇怪的日期以显示总体流量,因此这将比您的示例有更多的记录 这是输出:

StartDate   EndDate
Jul  1 2011     Sep 30 2011 
Oct  1 2011     Dec 31 2011 
Jan  1 2012     Mar 31 2012 
Apr  1 2012     Jun 30 2012 
Jul  1 2012     Sep 30 2012 
Oct  1 2012     Dec 31 2012 
Jan  1 2013     Mar 31 2013 
Apr  1 2013     Jun 30 2013 
Jul  1 2013     Sep 30 2013 
Oct  1 2013     Dec 31 2013 
Jan  1 2014     Mar 31 2014 
Apr  1 2014     Jun 30 2014 
Jul  1 2014     Sep 30 2014 
Oct  1 2014     Dec 31 2014 
Jan  1 2015     Mar 31 2015 

答案 2 :(得分:0)

SELECT TOP 1
  --first day of next quarter    
 CONVERT(VARCHAR(12),DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 1, 0)) as StartDate,
  --last day of next quarter
 CONVERT(VARCHAR(12),DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +2, 0))) as EndDate,
  --first day 2 quarters ahead
 CONVERT(VARCHAR(12),DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 2, 0)) as StartDate,
  --last day, 2 quarters ahead
 CONVERT(VARCHAR(12),DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +3, 0))) as EndDate,
  --first day 3 quarters ahead
 CONVERT(VARCHAR(12),DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) + 3, 0)) as StartDate,
  --last day 3 quarters ahead
 CONVERT(VARCHAR(12),DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, MAX(Time_Stamp)) +4, 0))) as EndDate
 FROM Survey
 Where MainHospital = 'Hospital1'
 GROUP BY Time_Stamp
 ORDER BY Time_Stamp DESC

获取日期格式,但不是2列

    StartDate    EndDate      StartDate    EndDate      StartDate    EndDate
------------ ------------ ------------ ------------ ------------ ------------
Jul  1 2014  Sep 30 2014  Oct  1 2014  Dec 31 2014  Jan  1 2015  Mar 31 2015