SQL 根据 from 和 to date 查询出生日期和月份

SQL query for the birth date and month based on from and to date

问题总结:获取所有生日在两个日期之间的员工。

我有一个 Employee table 有名字和出生日期

Emp 出生日期
ABC 1991-03-10 00:00:00.000
XYZ 1992-12-1 00:00:00.000
阿杰姆 1992-08-20 00:00:00.000
RNM 1991-07-10 00:00:00.000

我正在寻找查询以获取出生日期介于从和到日期之间的所有员工而不检查年份。

应该return 1条记录

Emp 出生日期
ABC 1991-03-10 00:00:00.000

结果应该是:

Emp 出生日期
ABC 1991-03-10 00:00:00.000
XYZ 1992-12-1 00:00:00.000
阿杰姆 1992-08-20 00:00:00.000
RNM 1991-07-10 00:00:00.000

我想我有办法找到所需范围内的生日,同时考虑到年份界限:

DECLARE @employee  TABLE(
        Emp  VARCHAR(100),
        DOB datetime
)

INSERT INTO @employee SELECT 'ABC','1991-03-10'
INSERT INTO @employee SELECT 'XYZ','1992-12-01'
INSERT INTO @employee SELECT 'AJM','1992-08-20'
INSERT INTO @employee SELECT 'RNM','1991-07-10'

 DECLARE @StartDate datetime = '2020-12-01';
DECLARE @EndtDate datetime = '2020-12-01';

select * 
from @employee
where 
((Floor(DateDiff(dd,dob,@EndtDate) / 365.25))-(Floor(DateDiff(dd,dob,@StartDate) / 365.25)) = 1)
OR (MONTH(@StartDate)=MONTH(DOB) AND DAY(@StartDate)=DAY(DOB))

Db fiddle example

对于未来的每个人: 您可以根据需要设置 @StartDate@EndtDate

之间的简单 select 日期

代码如下:

  DECLARE @employee  TABLE(
            Emp  VARCHAR(100),
            DOB datetime
    )
    
    INSERT INTO @employee SELECT 'ABC','1991-03-01'
    INSERT INTO @employee SELECT 'XYZ','1992-12-01'
    INSERT INTO @employee SELECT 'AJM','1992-08-20'
    INSERT INTO @employee SELECT 'RNM','1991-07-10'

    DECLARE @StartDate datetime = '2020-03-01';
    DECLARE @EndtDate datetime = '2021-01-15';


    DECLARE @employeeResultFinal  TABLE(
            Emp  VARCHAR(100),
            DOB datetime
    )
    


    IF(DAY(@StartDate)) =DAY(@EndtDate)
    begin
    
        IF(MONTH(@StartDate)) =MONTH(@EndtDate)
        begin 
        
            IF(YEAR(@StartDate)) !=YEAR(@EndtDate)
            begin
                 select * from @employee 
                 goto Result;
            end
        end

    end




    IF(@StartDate) = @EndtDate
    begin

        
        select * from @employee 
        where MONTH(DOB) = MONTH(@EndtDate)
        and DAY(DOB) = DAY(@EndtDate) 
        goto Result;
        
    end


    IF(YEAR(@StartDate) != YEAR(@EndtDate))
    begin
    -- when there is a more than 1 year diff

    SELECT *
    FROM @employee
    WHERE (Month(DOB) >= MONTH(@StartDate) AND Day(DOB) >= DAY(@StartDate)) 
    OR (Month(DOB) <= MONTH(@EndtDate) AND Day(DOB) <= DAY(@EndtDate))  
    goto Result;
    end

        IF(YEAR(@StartDate) = YEAR(@EndtDate))
    begin
    -- it select if its only the same year
    INSERT INTO @employeeResultFinal
    SELECT*
    FROM @employee
    WHERE  MONTH(DOB) >=MONTH(@StartDate)
    and MONTH(DOB) <= MONTH(@EndtDate)
    end


    --delete  record if the  same month is in DOB and EndDate and the day is higher in DOB 

      delete from @employeeResultFinal
      where 
      MONTH(DOB) = MONTH(@EndtDate)
      and DAY(DOB) > DAY(@EndtDate)


      select * from @employeeResultFinal

      Result:

DbFiddleDemo