如何根据开始日期和结束日期计算季节

时间:2015-02-26 20:01:01

标签: sql-server sql-server-2008 sql-function qsqlquery

我想在这个临时表中添加一个季节描述和季节 基于日期选择参数startDate和endDate与catchdate进行比较 seaon定义每年的季节从10月1日开始,到次年9月30日结束 例如,如果捕获日期在2013年10月1日至2014年9月30日之间,那将是2013年季节,季节描述将是2013年10月1日至2014年9月30日 我正在考虑编写功能以根据日期选择获得季节,但不知道如何开始。这是我的临时表

enter code here
If(OBJECT_ID('tempdb..#tempcatch') Is Not Null)/****** Script for SelectTopNRows command from SSMS  ******/
  drop table #tempcatch;
create table #tempcatch
(
        [id] int ,
        [customerId]   int,     
        [catchDate]  datetime,
        [submissionDate]  datetime,
        [speciesId] int,              
        [catchStatusId] int,
        [approvedDate]  datetime

  )
INSERT INTO #tempcatch VALUES (1, 123,'01-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (2, 124,'05-30-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (3, 125, '06-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (4, 126, '07-12-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (5, 123, '08-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (6, 124, '10-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (7, 125, '10-28-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (8, 126, '09-10-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (9, 127, '01-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (10, 128, '02-15-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (11, 129, '03-10-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (12, 130, '03-11-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (13, 131,'04-20-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (14, 132,'06-29-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (15, 133, '08-25-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (16, 134, '07-12-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (17, 135, '08-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (18, 136, '10-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (19, 137, '12-28-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (20, 138, '01-10-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (21, 139, '01-12-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (22, 140, '02-09-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (23, 141, '02-20-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (24, 142, '02-26-2015','',1,1,'' );


select id,customerId,catchDate,submissionDate,speciesId,catchStatusId,approvedDate,'seasonDescription'='','Season'=''

 from #tempcatch 

enter image description here

2 个答案:

答案 0 :(得分:1)

一个简单的案例表达应该这样做。

SELECT id
       , customerId
       , catchDate
       , submissionDate
       , speciesId
       , catchStatusId
       , approvedDate
       , '' as 'seasonDescription'
       , case when MONTH(catchDate) >= 10 then YEAR(catchDate) + 1 else YEAR(catchDate) end as Season
FROM   #tempcatch 

答案 1 :(得分:1)

尝试此查询:

SELECT *,
        seasonDescription = CONVERT(VARCHAR(20), startDate, 107) + ' - ' + CONVERT(VARCHAR(20), DATEADD(DAY, -1, DATEADD(YEAR, 1, startDate)), 107),
        season = YEAR(startDate)
FROM (
    SELECT *, DATEADD(MONTH, (DATEDIFF(MONTH, {d '1900-10-01'}, catchDate) / 12) * 12, {d '1900-10-01'}) AS startDate
    FROM #tempcatch
) y