【问题标题】:SQL query for the birth date and month based on from and to dateSQL 根据 from 和 to 日期查询出生日期和月份
【发布时间】:2021-10-08 03:01:48
【问题描述】:

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

我有一个Employee 表格,上面有姓名和出生日期

Emp DOB
ABC 1991-03-10 00:00:00.000
XYZ 1992-12-1 00:00:00.000
AJM 1992-08-20 00:00:00.000
RNM 1991-07-10 00:00:00.000

我正在寻找查询以获取所有员工的生日在 from 和 to date 之间而不检查年份。

  • 从日期 - 2020 年 1 月 1 日
  • 迄今为止 - 2020 年 3 月 31 日

它应该返回 1 条记录

Emp DOB
ABC 1991-03-10 00:00:00.000
  • 从日期 - 2020 年 3 月 1 日
  • 至今 - 2021 年 1 月 15 日

结果应该是:

Emp DOB
ABC 1991-03-10 00:00:00.000
XYZ 1992-12-1 00:00:00.000
AJM 1992-08-20 00:00:00.000
RNM 1991-07-10 00:00:00.000

【问题讨论】:

  • 如果有人将其标记为否定,请添加评论,以便我改进我的问题
  • 这些是DOB 列中的实际值吗?如果是这样,我强烈建议您修复架构并使用真正的 DATE 值。
  • 已更新为实际值

标签: sql sql-server-2008


【解决方案1】:

对于未来的每个人: 您可以根据需要轻松选择日期,只需设置 @StartDate@EndtDate

代码如下:

  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

【讨论】:

  • 声明 StartDate datetime = '2020-10-01';声明 EndtDate 日期时间 = '2021-10-10';试试这个
  • 谁正常查谁生日超过1年?这是愚蠢和无用的,这就是为什么我不将它包含在脚本中
【解决方案2】:

考虑到年份边界,我想我有办法在所需范围内找到生日:

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

【讨论】:

  • 嗨@Stu,非常接近几乎所有工作正常的案例,刚刚检查了一个缺少的案例是 startdate= 2020-12-01 它应该返回相同的日期记录,即 - 'XYZ','1992-12 -01'
  • 现在我刚刚添加了一个条件来检查 startdate 日期和月份是否适用于所有情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
相关资源
最近更新 更多