• 查询指定日期段内过生日的人员.sql


    --测试数据
    DECLARE @t TABLE(ID int,Name varchar(10),Birthday datetime)
    INSERT @t SELECT 1,'aa','1999-01-01'
    UNION ALL SELECT 2,'bb','1996-02-29'
    UNION ALL SELECT 3,'bb','1934-03-01'
    UNION ALL SELECT 4,'bb','1966-04-01'
    UNION ALL SELECT 5,'bb','1997-05-01'
    UNION ALL SELECT 6,'bb','1922-11-21'
    UNION ALL SELECT 7,'bb','1989-12-11'

    DECLARE @dt1 datetime,@dt2 datetime

    --查询 2003-12-05 至 2004-02-28 生日的记录
    SELECT @dt1='2003-12-05',@dt2='2004-02-28'
    SELECT * FROM @t
    WHERE DATEADD(Year,DATEDIFF(Year,Birthday,@dt1),Birthday)
            BETWEEN @dt1 AND @dt2
        OR DATEADD(Year,DATEDIFF(Year,Birthday,@dt2),Birthday)
            BETWEEN @dt1 AND @dt2
    /*--结果
    ID         Name       Birthday
    ---------------- ---------------- --------------------------
    1           aa         1999-01-01 00:00:00.000
    7           bb         1989-12-11 00:00:00.000
    --*/

    --查询 2003-12-05 至 2006-02-28 生日的记录
    SET @dt2='2006-02-28'
    SELECT * FROM @t
    WHERE DATEADD(Year,DATEDIFF(Year,Birthday,@dt1),Birthday)
            BETWEEN @dt1 AND @dt2
        OR DATEADD(Year,DATEDIFF(Year,Birthday,@dt2),Birthday)
            BETWEEN @dt1 AND @dt2
    /*--查询结果
    ID         Name       Birthday
    ---------------- ----------------- --------------------------
    1           aa         1999-01-01 00:00:00.000
    2           bb         1996-02-29 00:00:00.000
    7           bb         1989-12-11 00:00:00.000
    --*/
  • 相关阅读:
    【数组】Unique Paths II
    【数组】Unique Paths
    【数组】word search
    购物网站布局实战
    Javascript显示和隐式类型转换
    JS检测数据类型
    从setTimeout谈js运行机制
    0.1 + 0.2 = 0.30000000000000004怎样理解
    (译)详解javascript立即执行函数表达式(IIFE)
    Redis
  • 原文地址:https://www.cnblogs.com/shihao/p/2507279.html
Copyright © 2020-2023  润新知