如何从SQL中的日期获取日历季度

时间:2012-06-21 15:05:58

标签: sql sql-server tsql sql-server-2005

我在列中有不同的日期。例如:

20080102
20070821

我想在年份和日历季度转换这些日期。如,

Year      Quarter
2008      2008-Q1
2007      2007-Q3

我可以通过以下方式获得第一列:

select left(date,4) as year from table 

如何制作第二列?

17 个答案:

答案 0 :(得分:78)

SELECT DATEPART(QUARTER, @date)

这会返回@date的四分之一,假设@dateDATETIME

答案 1 :(得分:12)

SELECT DATENAME(Quarter, CAST(CONVERT(VARCHAR(8), datecolumn) AS DATETIME))

答案 2 :(得分:4)

我是这样做的。非常简短,不依赖临时表。

CAST(year(TheDate) AS char(4)) + '-Q' + 
CAST(CEILING(CAST(month(TheDate) AS decimal(9,2)) / 3) AS char(1))

举个例子:

SELECT convert(char(10), getdate(), 101) AS TheDate, 
CAST(year(getdate()) AS char(4)) + '-Q' + 
    CAST(CEILING(CAST(month(getdate()) AS decimal(4,2)) / 3) AS char(1)) AS SelectQuarter 

这将返回:

TheDate    SelectQuarter
---------- -------------
07/10/2013 2013-Q3

显然字符串本身可以更改为适合您自己的格式。希望这有用。

答案 3 :(得分:3)

这是另一种选择。使用CTE定义季度的月份,然后加入它以确定季度:

WITH Quarters AS (
   SELECT Q = 'Q1', MonthBegin = 1, MonthEnd = 3 UNION
   SELECT Q = 'Q2', MonthBegin = 4, MonthEnd = 6 UNION
   SELECT Q = 'Q3', MonthBegin = 7, MonthEnd = 9 UNION
   SELECT Q = 'Q4', MonthBegin = 10, MonthEnd = 12
)
SELECT
   [Year] = DATEPART(yyyy, CONVERT(DATETIME, Dates.[date])),
   [Quarter] = CONVERT(VARCHAR(4), DATEPART(yyyy, CONVERT(DATETIME, Dates.[date]))) + '-' + q.Q
FROM
   (VALUES
       ('20080102'),
       ('20070821')
   ) AS Dates ([date])
   INNER JOIN Quarters q ON
      DATEPART(m, CONVERT(DATETIME, Dates.[date])) >= q.MonthBegin AND
      DATEPART(m, CONVERT(DATETIME, Dates.[date])) <= q.MonthEnd;

返回:

Year  Quarter
----- ----------
2008  2008-Q1
2007  2007-Q3

SQL Fiddle

处理int类型的列类型(2014年4月23日):

WITH Quarters AS (
    SELECT Q = 'Q1', MonthBegin = 1, MonthEnd = 3 UNION
    SELECT Q = 'Q2', MonthBegin = 4, MonthEnd = 6 UNION
    SELECT Q = 'Q3', MonthBegin = 7, MonthEnd = 9 UNION
    SELECT Q = 'Q4', MonthBegin = 10, MonthEnd = 12
)
SELECT
    [Year] = DATEPART(yyyy, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))),
    [Quarter] = CONVERT(VARCHAR(4), DATEPART(yyyy, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date])))) + '-' + q.Q
FROM
    (VALUES
        (20080102),
        (20070821)
    ) AS Dates ([date])
    INNER JOIN Quarters q ON
        DATEPART(m, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))) >= q.MonthBegin AND
        DATEPART(m, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))) <= q.MonthEnd;

答案 4 :(得分:2)

由于您的日期字段数据位于int,因此您需要将其转换为日期时间:

declare @date int
set @date = 20080102

SELECT Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + substring(cast(@date as char(8)), 7, 2) as datetime)) as Quarter

SELECT Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + right(@date, 2) as datetime)) as quarter

然后,如果您想要添加Q1

SELECT left(@date, 4) + '-Q' + Convert(varchar(1), Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + right(@date, 2) as datetime))) as quarter

我的建议是将您的date数据存储为datetime,这样您就无需执行这些转换。

答案 5 :(得分:1)

假设字段数据类型为INT,字段名称为“mydate”。 在OP问题中,转换为字符串时的INT日期值是ISO日期文字。

select DatePart(QUARTER, cast(cast(mydate as char(8)) as date))

如果使用较旧的服务器版本,则可以使用datetime。

答案 6 :(得分:1)

我是这样做的(我正在使用SQL Server):

SELECT 'Q' + cast(DATEPART(QUARTER, GETDATE()) as varchar(1)) + ' - ' + cast(DATEPART(YEAR, GETDATE()) as varchar(4)) AS 'Date Quarter'

答案 7 :(得分:1)

要获得您要求的确切输出,您可以使用以下内容:

CAST(DATEPART(YEAR, @Date) AS NVARCHAR(10)) + ' - Q' + CAST(DATEPART(QUARTER, @Date) AS NVARCHAR(10))

这将为您提供如下输出: &#34; 2015 - Q1&#34;,&#34; 2013 - Q3&#34;等

答案 8 :(得分:1)

SELECT
   Q.DateInQuarter,
   D.[Year],
   Quarter = D.Year + '-Q'
      + Convert(varchar(1), ((Q.DateInQuarter % 10000 - 100) / 300 + 1))
FROM
   dbo.QuarterDates Q
   CROSS APPLY (
      VALUES (Convert(varchar(4), Q.DateInQuarter / 10000))
   ) D ([Year])
;

See a Live Demo at SQL Fiddle

答案 9 :(得分:1)

很好的借口用CONVERT捣乱。可能更漂亮的方式:

live test on SQLfiddle here

create table the_table 
(
  [DateKey] INT,
)

insert into the_table
values
(20120101),
(20120102),
(20120201),
(20130601)


WITH myDateCTE(DateKey, Date) as
  (
    SELECT 
      DateKey
      ,[Date] = CONVERT(DATETIME, CONVERT(CHAR(8),DateKey),112) 
    FROM the_table
   )

SELECT 
  t.[DateKey]
  , m.[Date]
  , [QuarterNumber] = CONVERT(VARCHAR(20),Datepart(qq,Date))
  , [QuarterString] = 'Q' + CONVERT(VARCHAR(20),Datepart(qq,Date))
  , [Year] = Datepart(yyyy,Date) 
  , [Q-Yr] = CONVERT(VARCHAR(2),'Q' + CONVERT(VARCHAR(20),Datepart(qq,Date))) + '-' + CONVERT(VARCHAR(4),Datepart(yyyy,Date))  
FROM 
  the_table t
  inner join myDateCTE m
    on 
    t.DateKey = m.DateKey

答案 10 :(得分:0)

您必须将整数转换为char(8)然后转换为datetime。 然后把它包起来 选择DATEPART(QUARTER,[date])

然后您必须将上述内容转换为字符并添加' - '+年(也转换为字符)

上面的算术溢出是由于省略了初始转换为字符类型。

我倾向于在可能的情况下使用视图抽象转换为日期时间,然后在需要时使用季度函数和字符转换。

答案 11 :(得分:0)

在这里您可以看到更多选择之一:

SELECT CASE
         WHEN @TODAY BETWEEN @FY_START AND DATEADD(M, 3, @FY_START) THEN 'Q1'
         WHEN @TODAY BETWEEN DATEADD(M, 3, @FY_START) AND DATEADD(M, 6, @FY_START) THEN 'Q2'
         WHEN @TODAY BETWEEN DATEADD(M, 6, @FY_START) AND DATEADD(M, 9, @FY_START) THEN 'Q3'
         WHEN @TODAY BETWEEN DATEADD(M, 9, @FY_START) AND DATEADD(M, 12, @FY_START) THEN 'Q4'
       END

答案 12 :(得分:0)

试试这个

SELECT  CONCAT (TO_CHAR(sysdate,'YYYY-'),concat ('Q',TO_CHAR(sysdate,'Q') ))from dual

sysdate替换为您自己的日期类型格式的列名,将dual替换为您的表名

答案 13 :(得分:0)

使用函数MONTH将月份返回为数字,我们可以轻松地计算季度。

select date, CEILING((MONTH(date) * 4) / 12) quarter from dual

答案 14 :(得分:0)

declare @TempTable table([Date] datetime)

insert into @TempTable([Date])
values('2008-01-02'),('2007-08-21')
                      
select datepart(year, [Date]) as [year]
,convert(varchar(10),datepart(year, [Date])) + '-Q' + convert(varchar(3),datename(quarter, [Date])) as quarter_with_year
,datefromparts(datepart(year, [Date]),(convert(varchar(3),datename(quarter, [Date])) * 3)-2,1) as quarter_startdate
,eomonth(datefromparts(datepart(year, [Date]),convert(varchar(3),datename(quarter, [Date])) * 3,1)) as quarter_enddate
FROM @TempTable

这将返回年,季度和季度的开始结束日期。

答案 15 :(得分:0)

select 
  your_date, 
  CONCAT (TO_CHAR(your_date, 'YYYY-'),concat ('Q', date_part(quarter, your_date) ))
from table;

答案 16 :(得分:-2)

尝试以下操作:

CONCAT(datepart(yyyy,DATE),'-', DATEPART(qq,DATE))

它返回:

yyyy-q

示例:2017-3的{​​{1}}