zoukankan      html  css  js  c++  java
  • Sql语句 生日提醒

    /*
    --Desc: 在计算员工当前日期的生日时,需要考虑闰年的问题,
    一般在2月29号出生的人在非闰年时生日在3月1号过。
    --Environment: SQL SERVER 2008
    --Author: Learnsql
    */

    USE Tempdb;
    GO
    --创建测试表
    CREATE TABLE #Employee
    (
    [Name] NVARCHAR(10),
    BirthDay
    DATETIME
    );
    GO

    --插入数据
    INSERT INTO #Employee([Name],BirthDay) VALUES('Tom','1970-1-3');
    INSERT INTO #Employee([Name],BirthDay) VALUES('Jim','1971-11-23');
    INSERT INTO #Employee([Name],BirthDay) VALUES('Lucy','1972-2-29');
    INSERT INTO #Employee([Name],BirthDay) VALUES('Bob','1979-3-15');
    INSERT INTO #Employee([Name],BirthDay) VALUES('Jack','1985-5-10');
    GO

    -- 提前通知天数
    DECLARE @ReminderDay INT
    SET @ReminderDay = 100;

    --生日计算
    WITH Emp1([Name],BirthDay,DiffYear,Today) AS --计算当前日期和生日相差的年数
    (
    SELECT [Name], BirthDay,
    DATEDIFF(YEAR, BirthDay, GETDATE()) AS DiffYear,
    CAST(CONVERT(NVARCHAR(8), GETDATE(), 112) AS DATETIME) AS Today
    FROM #Employee
    ),
    Emp2(
    [Name],BirthDay,Today,BDCur,BDNxt) AS --计算当前生日及下一年生日
    (
    SELECT [Name],BirthDay, Today,
    DATEADD(YEAR, DiffYear, BirthDay) AS BDCur,
    DATEADD(YEAR, DiffYear + 1, BirthDay) AS BDNxt
    FROM Emp1
    ),
    Emp3(
    [Name],BirthDay,Today,BDCur,BDNxt) AS --对闰年生日处理
    (
    SELECT [Name],BirthDay, Today,
    BDCur
    + CASE WHEN DAY(BirthDay) = 29 AND DAY(BDCur) = 28
    THEN 1 ELSE 0 END AS BDCur,
    BDNxt
    + CASE WHEN DAY(BirthDay) = 29 AND DAY(BDNxt) = 28
    THEN 1 ELSE 0 END AS BDNxt
    FROM Emp2
    )

    SELECT #Employee.* FROM (
    SELECT [Name], BirthDay,
    CASE WHEN BDCur >= Today THEN BDCur ELSE BDNxt END AS BirthDate,
    DATEDIFF(DAY,Today,CASE WHEN BDCur >= Today THEN BDCur ELSE BDNxt END) AS DiffDay
    FROM Emp3) Emp4
    INNER JOIN #Employee
    ON Emp4.Name = #Employee.Name
    WHERE Emp4.DiffDay < @ReminderDay
    ;
  • 相关阅读:
    Java 基础:继承中的执行顺序
    Java 基础:抽象类与接口
    ElasticSearch : 基础简介
    Java GUI : 实现排序算法的动态演示
    Java GUI:将JPanel添加进JScrollPane
    菜鸟的算法入门:java的链表操作
    Docker与Tomcat:去掉项目名称进行访问
    vue输入框的搜索返回信息高亮显示
    position: static 和 position: unset
    vue css /deep/ 用法
  • 原文地址:https://www.cnblogs.com/litsword/p/2153241.html
Copyright © 2011-2022 走看看