sql 计算生日 - 使用“EXEC UpdateBirthDays”创建sql作业并每天安排它。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 计算生日 - 使用“EXEC UpdateBirthDays”创建sql作业并每天安排它。相关的知识,希望对你有一定的参考价值。
USE [KDSkladi_MSCRM]
GO
/****** Object: UserDefinedFunction [dbo].[calculateAge] Script Date: 05.04.2016 13:25:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[calculateAge]
(
-- Add the parameters for the function here
@birthdate date
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @Result int
-- Add the T-SQL statements to compute the return value here
SELECT @Result=FLOOR(DATEDIFF(day,@birthdate,GETUTCDATE()) / 365.242199)
-- Return the result of the function
RETURN @Result
END
/****** Object: UserDefinedFunction [dbo].[getNextBirthDate] Script Date: 05.04.2016 13:25:39 ******/
CREATE FUNCTION [dbo].[getNextBirthDate]
(
-- Add the parameters for the function here
@birthdate datetime
)
RETURNS datetime
AS
BEGIN
-- Declare the return variable here
DECLARE @Result datetime
DECLARE @todays_date date = GETUTCDATE()
SET @birthdate=DATEADD(HOUR,2,@birthdate)
-- Add the T-SQL statements to compute the return value here
SELECT @Result=
DATEADD(YEAR,
-- Number of whole years between date of birth and today's date plus 1
CASE
-- Month of date of birth greater than month of today's date
WHEN DATEPART(MONTH, @birthdate) > DATEPART(MONTH, @todays_date)
THEN DATEDIFF(YEAR, @birthdate, @todays_date)
-- Month of date of birth same as month of today's date and day of date of birth greater than day of today's date
WHEN DATEPART(MONTH, @birthdate) = DATEPART(MONTH, @todays_date) AND DATEPART(DAY, @birthdate) > DATEPART(DAY, @todays_date)
THEN
CASE
-- Date of birth is on a leap day and this year is not a leap year
WHEN DATEPART(MONTH, @birthdate) = 2 AND DATEPART(DAY, @birthdate) = 29
AND NOT (DATEPART(YEAR, @todays_date) % 400 = 0 OR (DATEPART(YEAR, @todays_date) % 100 <> 0 AND DATEPART(YEAR, @todays_date) % 4 = 0))
THEN DATEDIFF(YEAR, @birthdate, @todays_date) + 1
-- Else
ELSE DATEDIFF(YEAR, @birthdate, @todays_date)
END
-- Else
ELSE
CASE
WHEN DATEPART(MONTH, @birthdate) = DATEPART(MONTH, @todays_date) AND DATEPART(DAY, @birthdate) = DATEPART(DAY, @todays_date)
THEN
DATEDIFF(YEAR, @birthdate, @todays_date)
ELSE
DATEDIFF(YEAR, @birthdate, @todays_date) + 1
END
END,
@birthdate)
-- Return the result of the function
SET @Result=DATEADD(HOUR,-2,@Result)
RETURN @Result
END
/****** Object: StoredProcedure [dbo].[UpdateBirthDays] Script Date: 05.04.2016 13:23:34 ******/
CREATE PROCEDURE [dbo].[UpdateBirthDays]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE C
SET --C.avt_dayofbirth=DATEPART(day, C.birthdate),
--C.avt_monthofbirth=DATEPART(month, C.birthdate),
--C.avt_yearofbirth=DATEPART(year, C.birthdate),
C.avt_age=[dbo].[calculateAge](C.birthdate),
C.avt_nextbirthday=[dbo].[getNextBirthDate](C.birthdate)
FROM ContactBase AS C
END
以上是关于sql 计算生日 - 使用“EXEC UpdateBirthDays”创建sql作业并每天安排它。的主要内容,如果未能解决你的问题,请参考以下文章
PL/SQL 程序来计算年龄以及你的生日年份是不是是闰年[关闭]
求高手解决:SQL语句查询本周内生日的同学,以及本月内生日的同学....