如何获得多个日期 [英] how to get multiple date
问题描述
ID Startdate Enddate
105 2009-04-01 00:00:00.000 2010-03-31 00:00:00.000
108 2010-04-01 00 :00:00.000 2011-03-31 00:00:00.000
113 2011-04-01 00:00:00.000 2012-03-31 00:00:00.000
115 2012-04-01 00:00:00.000 2013-03-31 00:00:00.000
120 2013-04-01 00:00:00.000 2014-03-31 00:00: 00.000
121 2014-04-01 00:00:00.000 2015-03-31 00:00:00.000
122 2015-04-01 00:00:00.000 2016 -03-31 00:00:00.000
i有这些数据,当我今天通过日期时,我会从2015年开始获取startdate,并根据当年开始瞄准2016年和2017年
表示我sholud得到三个日期当前前和未来
要获取当前日期的年份使用
SELECT 年份(GetDate())
要选择根据源字段的年份
SELECT * FROM [date_table]
WHERE 年(StartDate)=年(GetDate())
选择接下来的两年
SELECT * FROM [date_table]
WHERE 年(StartDate)=年(GetDate())
或年(StartDate) )= 1 +年(GetDate())
或年(StartDate)= 2 +年(GetDate())
您可以获取当年,上一年的开始日期,明年
声明 @ Today datetime
set @ Today = getdate()
SELECT DATEADD(yy,DATEDIFF(yy, 0 , @今天) - 1, 0 ) AS StartOfPreviousYear,
DATEADD(yy, DATEDIFF(yy, 0 , @ Today ), 0 ) AS StartOfCurrentYear,
DATEADD(yy,DATEDIFF(yy, 0 ,< span class =code-sdkkeyword> @ Today )+ 1, 0 ) AS StartOfNextYear
ID Startdate Enddate
105 2009-04-01 00:00:00.000 2010-03-31 00:00:00.000
108 2010-04-01 00:00:00.000 2011-03-31 00:00:00.000
113 2011-04-01 00:00:00.000 2012-03-31 00:00:00.000
115 2012-04-01 00:00:00.000 2013-03-31 00:00:00.000
120 2013-04-01 00:00:00.000 2014-03-31 00:00:00.000
121 2014-04-01 00:00:00.000 2015-03-31 00:00:00.000
122 2015-04-01 00:00:00.000 2016-03-31 00:00:00.000
i have this data and when i pass today date i sholud get startdate starting from 2015 and startdate staring fom 2016 and 2017 based on current year
means i sholud get three date current previous and future
To get the year of the current date use
SELECT YEAR(GetDate())
To select based on the year of the source field
SELECT * FROM [date_table] WHERE YEAR(StartDate) = YEAR(GetDate())
To select the next two years as well
SELECT * FROM [date_table] WHERE YEAR(StartDate) = YEAR(GetDate()) OR YEAR(StartDate) = 1 + YEAR(GetDate()) OR YEAR(StartDate) = 2 + YEAR(GetDate())
You can get the start date of current year, previous year, next year
declare @Today datetime set @Today=getdate() SELECT DATEADD(yy, DATEDIFF(yy,0,@Today)-1, 0) AS StartOfPreviousYear, DATEADD(yy, DATEDIFF(yy,0,@Today), 0) AS StartOfCurrentYear, DATEADD(yy, DATEDIFF(yy,0,@Today)+1, 0) AS StartOfNextYear
这篇关于如何获得多个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!