简体   繁体   English

比较SQL Server数据库中的DateTime列

[英]Compare DateTime column in SQL Server database

I have a column of DateTime type (called "FinalisedDate") in a SQL Server 2008 table. 我在SQL Server 2008表中有一列DateTime类型(称为“ FinalizedDate”)。 The column contains records that can be dated back a few years ago. 该列包含可以追溯到几年前的记录。

I need to get those rows that are falling in "the current operational data". 我需要获取属于“当前操作数据”中的那些行。 It is defined as: 它定义为:

from the previous year's October 1st to May 1st of the present year if current date is before May 1st, or from the October 1st of the present year to May 1st of the next year if current date is after October 1st. 如果当前日期在5月1日之前,则从前一年的10月1日到5月1日;如果当前日期在10月1日之后,则从本年的10月1日到次年的5月1日。

For example, 例如,

FinalisedDate

2013-04-15 00:00:00.000
2013-07-01 00:00:00.000
2013-10-01 00:00:00.000    //the current operational data
2013-11-15 00:00:00.000    //the current operational data
2013-12-30 00:00:00.000    //the current operational data
2014-01-15 00:00:00.000    //the current operational data
2014-03-01 00:00:00.000    //the current operational data
2014-04-15 00:00:00.000    //the current operational data
2014-05-30 00:00:00.000
2014-09-01 00:00:00.000

Anyone can help with a solution to get all rows of the current operational year? 任何人都可以提供解决方案以获取当前运营年度的所有行吗? Thanks in advance. 提前致谢。

Cheers, Alex 干杯,亚历克斯

My suggestion is to take this approach. 我的建议是采用这种方法。 Start with two DateTime variables: 从两个DateTime变量开始:

declare @StartDate as datetime, @EndDate as datetime

Then look at the current date, and use conditional logic to assign values to those variables. 然后查看当前日期,并使用条件逻辑为这些变量分配值。 Then do this: 然后执行以下操作:

where FinalizedDate >= @StartDate
and FinalizedDate < Dateadd(day, 1, @EndDate)

You get dibs on solving the logic part on your own. 您会自己解决逻辑部分而感到困惑。

Edit starts here 编辑从这里开始

However, that's a bandaid solution. 但是,这是一个创可贴的解决方案。 You would benefit from a calendar table, ie a table with the date as the primary key and other fields as required. 您将受益于日历表,即以日期为主键和其他必填字段的表。 One of those fields would be OperationalYear and it would have values like '2013/14', '2014/15', etc. 这些字段之一将是OperationalYear,其值将为'2013/14','2014/15'等。

Then you could something like this: 然后,您可能会这样:

from thistable join CalendarTable on FinalizedDate = TheDate 
where OperationalYear = (subquery that gets OperationalYear from GetDate())

Peronally I'd work out the date range in another function and then simply do a between query but something like 我会定期在另一个函数中计算日期范围,然后简单地执行一次之间查询,但类似

Declare @startDate DateTime
Declare @endDate DateTime
Declare @offset int
set @Offset = 0
if Month(GetDate()) < 5
begin
  set @offset = -1
end

set @startDate = Convert(DateTime,Convert(VarChar(4),Year(GetDate())  - offset) + '1001',112)
set @endDate = DateSubtract(day,1,DateAdd(year,1,@startDate))
select * from MyTable Where FinalizedDate Between @startDate and @endDate

should do the job 应该做的工作

SELECT *
FROM MyTable
CROSS APPLY (
  SELECT CASE
    WHEN MONTH(currentdate) <   5 THEN YEAR(currentdate)-1
    WHEN MONTH(currentdate) >= 10 THEN YEAR(currentdate)
    END
  FROM (SELECT GETDATE()) t1(currentdate)
  ) t2(opyear)
WHERE FinalisedDate >= DATEFROMPARTS(opyear  ,10,1)
  AND FinalisedDate <  DATEFROMPARTS(opyear+1, 5,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM