51

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

20080102
20070821

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

Year      Quarter
2008      2008-Q1
2007      2007-Q3

我可以得到第一列:

select left(date,4) as year from table 

我怎样才能产生第二列?

4

17 回答 17

116
SELECT DATEPART(QUARTER, @date)

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

于 2013-08-15T17:19:41.503 回答
12
SELECT DATENAME(Quarter, CAST(CONVERT(VARCHAR(8), datecolumn) AS DATETIME))
于 2012-06-21T15:07:42.287 回答
9

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

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

显然,字符串本身可以更改以适合您自己的格式。希望这会有所帮助。

于 2013-07-10T15:53:18.263 回答
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小提琴

处理 int 的列类型(04/23/2014):

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;
于 2013-07-17T17:44:56.907 回答
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这样,这样您就不需要执行这些转换。

于 2012-06-21T16:20:32.380 回答
2

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

SELECT 'Q' + cast(DATEPART(QUARTER, GETDATE()) as varchar(1)) + ' - ' + cast(DATEPART(YEAR, GETDATE()) as varchar(4)) AS 'Date Quarter'
于 2019-02-07T20:46:08.893 回答
1

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

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

如果使用较旧的服务器版本,您可以使用日期时间。

于 2013-08-20T11:25:50.767 回答
1

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

在这里对 SQLfiddle 进行实时测试

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
于 2012-06-22T07:23:05.483 回答
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])
;

在 SQL Fiddle 上查看现场演示

于 2013-07-09T04:15:59.160 回答
1

要获得您请求的确切输出,您可以使用以下命令:

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

这将为您提供如下输出:“2015 - Q1”、“2013 - Q3”等。

于 2018-05-30T23:24:30.760 回答
0

试试这个

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

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

于 2013-07-09T03:46:44.043 回答
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
于 2016-12-23T10:43:19.007 回答
0
select 
  your_date, 
  CONCAT (TO_CHAR(your_date, 'YYYY-'),concat ('Q', date_part(quarter, your_date) ))
from table;
于 2021-05-18T19:01:29.953 回答
0

使用以MONTH数字形式返回月份的函数,我们可以轻松计算季度。

select date, CEILING((MONTH(date) * 4) / 12) quarter from dual
于 2019-12-24T11:33:07.317 回答
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

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

于 2020-09-16T13:22:59.733 回答
0

您必须将整数转换为 char(8),然后转换为日期时间。然后将其包装在 SELECT DATEPART(QUARTER, [date])

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

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

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

于 2016-10-12T09:05:50.460 回答
-2

尝试以下操作:

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

它返回:

yyyy-q

示例:2017-3对于2017-07-11

于 2020-03-05T15:57:27.100 回答