Created
September 17, 2014 14:44
-
-
Save sebastianwebber/0938263a22d6e4206b3e to your computer and use it in GitHub Desktop.
SQL Server function to return interval a date interval between 2 datetime fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IF EXISTS (SELECT * | |
FROM dbo.sysobjects | |
WHERE id = object_id (N'[dbo].[date_interval]') | |
AND OBJECTPROPERTY(id, N'IsTableFunction') = 1) | |
DROP FUNCTION [dbo].[date_interval] | |
GO | |
CREATE FUNCTION [dbo].[date_interval] ( @p_start datetime, @p_end datetime) | |
RETURNS @DATES TABLE ( generated_date datetime ) | |
AS | |
BEGIN | |
DECLARE @minutes_interval INT = datediff(mi, @p_start, @p_end); | |
INSERT INTO @DATES | |
SELECT @p_start | |
UNION ALL | |
SELECT | |
DATEADD(MINUTE, g.intvalue, @p_start) | |
FROM generate_series(1, @minutes_interval) AS g | |
RETURN | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment