我在一列中有不同的日期。例如:
20080102
20070821
我想将这些日期转换为年份和日历季度。例如,
Year Quarter
2008 2008-Q1
2007 2007-Q3
我可以得到第一列:
select left(date,4) as year from table
我怎样才能产生第二列?
我在一列中有不同的日期。例如:
20080102
20070821
我想将这些日期转换为年份和日历季度。例如,
Year Quarter
2008 2008-Q1
2007 2007-Q3
我可以得到第一列:
select left(date,4) as year from table
我怎样才能产生第二列?
SELECT DATEPART(QUARTER, @date)
这将返回 的四分之一@date
,假设@date
是DATETIME
。
SELECT DATENAME(Quarter, CAST(CONVERT(VARCHAR(8), datecolumn) AS DATETIME))
这是我的做法。非常简短,不依赖临时表。
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
显然,字符串本身可以更改以适合您自己的格式。希望这会有所帮助。
这是另一种选择。使用 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
处理 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;
由于您的日期字段数据在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
这样,这样您就不需要执行这些转换。
我是这样做的(我正在使用 SQL Server):
SELECT 'Q' + cast(DATEPART(QUARTER, GETDATE()) as varchar(1)) + ' - ' + cast(DATEPART(YEAR, GETDATE()) as varchar(4)) AS 'Date Quarter'
假设字段数据类型为 INT 和字段名称“mydate”。在 OP 问题中,转换为字符串时的 INT 日期值是 ISO 日期文字。
select DatePart(QUARTER, cast(cast(mydate as char(8)) as date))
如果使用较旧的服务器版本,您可以使用日期时间。
用 CONVERT 捣乱的好借口。可能更漂亮的方法:
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
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])
;
要获得您请求的确切输出,您可以使用以下命令:
CAST(DATEPART(YEAR, @Date) AS NVARCHAR(10)) + ' - Q' + CAST(DATEPART(QUARTER, @Date) AS NVARCHAR(10))
这将为您提供如下输出:“2015 - Q1”、“2013 - Q3”等。
试试这个
SELECT CONCAT (TO_CHAR(sysdate,'YYYY-'),concat ('Q',TO_CHAR(sysdate,'Q') ))from dual
sysdate
用您自己的列名替换为日期类型格式和dual
您的表名
在这里,您可以看到更多选择之一:
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
select
your_date,
CONCAT (TO_CHAR(your_date, 'YYYY-'),concat ('Q', date_part(quarter, your_date) ))
from table;
使用以MONTH
数字形式返回月份的函数,我们可以轻松计算季度。
select date, CEILING((MONTH(date) * 4) / 12) quarter from dual
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
这将返回年份、季度和季度的开始结束日期。
您必须将整数转换为 char(8),然后转换为日期时间。然后将其包装在 SELECT DATEPART(QUARTER, [date])
然后,您必须将上述内容转换为字符并添加“-”+年份(也转换为字符)
上面的算术溢出是由于省略了初始转换为字符类型造成的。
我倾向于在可能的情况下使用视图将转换抽象为日期时间,然后在需要时使用四分之一函数和字符转换。
尝试以下操作:
CONCAT(datepart(yyyy,DATE),'-', DATEPART(qq,DATE))
它返回:
yyyy-q
示例:2017-3
对于2017-07-11