By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE FUNCTION dbo.CalculateWarranty (
@purchase_date DATE
)
RETURNS INT
AS
BEGIN
DECLARE @warranty_duration INT;
DECLARE @warranty_remaining INT;
-- Set the warranty duration to 3 years (in days)
SET @warranty_duration = 1095;
IF @purchase_date Is NULL
return -1
-- Calculate the remaining warranty duration in days
SET @warranty_remaining = DATEDIFF(DAY, @purchase_date, GETDATE());
-- If the remaining warranty is within 3 years, return the remaining duration; otherwise, return NULL
IF @warranty_remaining <= @warranty_duration
RETURN @warranty_duration - @warranty_remaining;
-- Default return statement (return NULL if warranty is not within 3 years)
RETURN NULL;
END;
select dbo.CalculateWarranty(NULL);
(No column name) |
---|
-1 |
select dbo.CalculateWarranty(GETdate());
(No column name) |
---|
1095 |
select dbo.CalculateWarranty(DATEADD(day,-5,GETdate()));
(No column name) |
---|
1090 |