-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetNextBirthday.sql
42 lines (36 loc) · 955 Bytes
/
GetNextBirthday.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
CREATE FUNCTION [dbo].[TryDate]
(
@year as INT,
@month as INT,
@day as INT
)
RETURNS DATE AS
BEGIN
RETURN TRY_CONVERT(DATE,CONCAT(FORMAT(@year,'D4'), FORMAT(@month,'D2'), FORMAT(@day,'D2')))
END
GO
CREATE FUNCTION [dbo].[GetNextBirtyday]
(
@month as INT,
@day as INT
)
RETURNS DATE AS
BEGIN
DECLARE @today DATE = GetDATE();
DECLARE @year INT = YEAR(@today);
DECLARE @returnDate DATE = NULL;
IF ISNULL(@month,0) BETWEEN 1 AND 12 AND ISNULL(@day,0) BETWEEN 1 AND 31
BEGIN
IF (@month = 2 AND @day = 29)
BEGIN
SET @returnDate = ISNULL(dbo.TryDate(@year,@month,@day), DATEFROMPARTS(@year,@month,28))
IF @returnDate < @today
SET @returnDate = ISNULL(dbo.TryDate(@year+1,@month,@day), DATEFROMPARTS(@year+1,@month,28))
RETURN @returnDate
END
SET @returnDate = DATEFROMPARTS(@year, @month, @day)
IF @returnDate < @today
SET @returnDate = DATEFROMPARTS(@year+1, @month, @day)
END
RETURN @returnDate
END