日期比较和差异

Date compare and difference

我有 "Employee" table 和员工的 "StartDate"。

我想获取下个月工作周年纪念日的所有员工的列表。

到目前为止,我已经达到了这一点:

SELECT *
FROM Employee
WHERE DATEDIFF(DAY, DAY(StartDate), DAY(GETDATE())) = 30

...但这似乎不起作用。

像这样的东西可以正常工作。但是,where 子句中的函数可能会导致大型数据集出现问题:

declare     @day int = day(getdate())
            ,@month int = month(getdate())

if @month = 12 set @month = 1
else set @month = @month +1

SELECT 
    [columns]
  FROM [table]
where day(Startdate) = @day
and MONTH(Startdate) = @month

或者,如果您不关心这一天(我想您可能不关心),那么:

declare @month int = month(getdate())

if @month = 12 set @month = 1
else set @month = @month +1

SELECT 
    [columns]
  FROM [table]
and MONTH(Startdate) = @month

I want to get a list of all employees who are reaching their work anniversary next month.

首先找到下一个月的日期

DATEADD(MONTH, 1, GETDATE())

然后你找到下个月StartDate的员工

SELECT *
FROM   Employee
WHERE  DATEPART(MONTH, StartDate) = DATEPART(MONTH, DATEADD(MONTH, 1, GETDATE()))