Sql server TSQL基于日期在select语句中包括会计年度
我需要根据今天的日期将会计年度包括在我的select报表中,例如2016-2017年12月30日。财政年度从7月1日开始。截至2017年7月1日,应计算2017-2018年。我怎样才能做到这一点Sql server TSQL基于日期在select语句中包括会计年度,sql-server,tsql,ssms,Sql Server,Tsql,Ssms,我需要根据今天的日期将会计年度包括在我的select报表中,例如2016-2017年12月30日。财政年度从7月1日开始。截至2017年7月1日,应计算2017-2018年。我怎样才能做到这一点 ClaimNo 20161030 20161124 20160815 20170115 20180101 SELECT * FROM TABLE WHERE ClaimNo LIKE '2016%' OR ClaimNo LIKE '2017%'; 选择ClaimNo, 如果月份(转换(日期,索赔
ClaimNo
20161030
20161124
20160815
20170115
20180101
SELECT *
FROM TABLE
WHERE ClaimNo LIKE '2016%' OR ClaimNo LIKE '2017%';
选择ClaimNo,
如果月份(转换(日期,索赔号))借助一个特殊的理货表,我们可以很容易地计算会计年度。在这种情况下,我们将从1980年开始,并在今后50年中继续这样做。这两项都可以根据需要进行调整
DECLARE @table TABLE (ClaimNo varchar(25));
INSERT INTO @table (ClaimNo)
VALUES
('20161030'),
('20161124'),
('20160815'),
('20170115'),
('20180101');
Select A.ClaimNo
,FiscalYear = Str(Year(D1),4)+' - '+Str(Year(D2),4)
From @table A
Join (
Select Top 50 D1=cast(DateAdd(YY,Row_Number() Over (Order By Number)-1,'1980-07-01') as Date)
,D2=cast(DateAdd(YY,Row_Number() Over (Order By Number),'1980-06-30') as Date)
From master..spt_values
) D
on ClaimNo Between D1 and D2
返回
ClaimNo FiscalYear
20161030 2016 - 2017
20161124 2016 - 2017
20160815 2016 - 2017
20170115 2016 - 2017
20180101 2017 - 2018
注意:如果ClaimNo是int,只需将ON子句更改为
on cast(ClaimNo as varchar(8)) Between D1 and D2
是否要将会计年度包括在选择中,即使用会计年度创建另一列,或限制会计年度返回的行,例如,其中FiscalYear(ClaimNo)=1066
?提示:使用适当的软件(MySQL、Oracle、DB2等)和版本(例如,sql-server-2014
)标记数据库问题很有帮助。语法和特征的差异通常会影响答案。
DECLARE @table TABLE (ClaimNo varchar(25));
INSERT INTO @table (ClaimNo)
VALUES
('20161030'),
('20161124'),
('20160815'),
('20170115'),
('20180101');
Select A.ClaimNo
,FiscalYear = Str(Year(D1),4)+' - '+Str(Year(D2),4)
From @table A
Join (
Select Top 50 D1=cast(DateAdd(YY,Row_Number() Over (Order By Number)-1,'1980-07-01') as Date)
,D2=cast(DateAdd(YY,Row_Number() Over (Order By Number),'1980-06-30') as Date)
From master..spt_values
) D
on ClaimNo Between D1 and D2
ClaimNo FiscalYear
20161030 2016 - 2017
20161124 2016 - 2017
20160815 2016 - 2017
20170115 2016 - 2017
20180101 2017 - 2018
on cast(ClaimNo as varchar(8)) Between D1 and D2