SQLServerCentral.com / SQL Server 2005 / SQL Server Newbies / Working of the function / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comThu, 08 Oct 2015 16:22:44 GMT20RE: Working of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspx[quote][b]Jeff Moden (11/29/2013)[/b][hr]Not that many people would notice but you can get the code to be almost a third faster by eliminating the (-1) subtraction. Last but not least, the simplification actually instills a guarantee that you won't have an accidental overrun sometimes caused by the very rare but annoying delay as to when the WHERE clause is executed when you join the results of this function in an external query (Itzik Ben-Gan published that correction to his code just a couple of days after he first published the cascading CTE method used here).[code="sql"] CREATE FUNCTION dbo.ExplodeDates ( @pStartDate DATETIME ,@pEndDate DATETIME )RETURNS TABLE WITH SCHEMABINDING AS RETURN WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), E7(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d, E1 e, E1 f, E1 g) SELECT TheDate = @pStartDate UNION ALL --This eliminates the need for subtraction on the Row_Number SELECT TOP (DATEDIFF(dd,@pStartDate,@pEndDate)) --and addition here TheDate = DATEADD(dd,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),@pStartDate) FROM E7;[/code][/quote]How are you demonstrating the difference, Jeff? The results of a quick test show the opposite, but not by very much:[code="sql"]DECLARE @startdate DATE, @enddate DATE, @Blackhole DATE;SET @startdate = '00010101'SET @enddate = '99991231';PRINT '==Slow inline tally table==';PRINT CHAR(10)+'--Subtraction--';SET STATISTICS TIME ON;with N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rows,N2 as (SELECT 1 as n FROM N1 t1, N1 t2) -- 16 rows,N3 as (SELECT 1 as n FROM N2 t1, N2 t2) -- 256 rows,N4 as (SELECT 1 as n FROM N3 t1, N3 t2) -- 65,536 rows,N5 as (SELECT 1 as n FROM N4 t1, N4 t2) -- 4,294,967,296 rows,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N5)SELECT @Blackhole = thedate FROM ( SELECT thedate = DATEADD(day,num-1,@startdate) FROM nums WHERE num <= DATEDIFF(day, @startdate, @enddate) + 1) d;SET STATISTICS TIME OFF;PRINT CHAR(10)+'--UNION--';SET STATISTICS TIME ON;with N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rows,N2 as (SELECT 1 as n FROM N1 t1, N1 t2) -- 16 rows,N3 as (SELECT 1 as n FROM N2 t1, N2 t2) -- 256 rows,N4 as (SELECT 1 as n FROM N3 t1, N3 t2) -- 65,536 rows,N5 as (SELECT 1 as n FROM N4 t1, N4 t2) -- 4,294,967,296 rows,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N5)SELECT @Blackhole = thedate FROM ( SELECT thedate = @startdate UNION ALL SELECT DATEADD(day,num,@startdate) FROM nums WHERE num <= DATEDIFF(day, @startdate, @enddate)) d;SET STATISTICS TIME OFF;PRINT CHAR(10)+CHAR(10)+'==Fast IBG inline tally table==';PRINT CHAR(10)+'--Subtraction--';SET STATISTICS TIME ON;WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), E7(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d, E1 e, E1 f, E1 g)SELECT @Blackhole = thedate FROM ( SELECT TOP (1+DATEDIFF(dd,@StartDate,@EndDate)) TheDate = DATEADD(dd,ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1,@StartDate) FROM E7) d;SET STATISTICS TIME OFF;PRINT CHAR(10)+'--UNION--';SET STATISTICS TIME ON;WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), E7(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d, E1 e, E1 f, E1 g)SELECT @Blackhole = thedate FROM ( SELECT TheDate = @StartDate UNION ALL --This eliminates the need for subtraction on the Row_Number SELECT TOP (DATEDIFF(dd,@StartDate,@EndDate)) --and addition here TheDate = DATEADD(dd,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),@StartDate) FROM E7) d;SET STATISTICS TIME OFF;PRINT '=================================================================================';[/code]Mon, 02 Dec 2013 02:57:22 GMTChrisM@WorkRE: Working of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspxNot that many people would notice but you can get the code to be almost a third faster by eliminating the (-1) subtraction. Last but not least, the simplification actually instills a guarantee that you won't have an accidental overrun sometimes caused by the very rare but annoying delay as to when the WHERE clause is executed when you join the results of this function in an external query (Itzik Ben-Gan published that correction to his code just a couple of days after he first published the cascading CTE method used here).[code="sql"] CREATE FUNCTION dbo.ExplodeDates ( @pStartDate DATETIME ,@pEndDate DATETIME )RETURNS TABLE WITH SCHEMABINDING AS RETURN WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), E7(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c, E1 d, E1 e, E1 f, E1 g) SELECT TheDate = @pStartDate UNION ALL --This eliminates the need for subtraction on the Row_Number SELECT TOP (DATEDIFF(dd,@pStartDate,@pEndDate)) --and addition here TheDate = DATEADD(dd,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),@pStartDate) FROM E7;[/code]Fri, 29 Nov 2013 14:05:49 GMTJeff ModenRE: Working of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspxNote that you don't need CTE N6:[code="sql"]DECLARE @startdate DATE, @enddate DATESET @startdate = '00010101'SET @enddate = '99990101';with N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rows,N2 as (SELECT 1 as n FROM N1 t1, N1 t2) -- 16 rows,N3 as (SELECT 1 as n FROM N2 t1, N2 t2) -- 256 rows,N4 as (SELECT 1 as n FROM N3 t1, N3 t2) -- 65,536 rows,N5 as (SELECT 1 as n FROM N4 t1, N4 t2) -- 4,294,967,296 rows--,N6 as (SELECT 1 as n FROM N5 t1, N5 t2) -- 18,446,744,073,709,551,616 rows!!,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N5)SELECT DATEADD(day,num-1,@startdate) as thedateFROM numsWHERE num <= DATEDIFF(day, @startdate, @enddate) + 1[/code]Fri, 29 Nov 2013 07:09:49 GMTChrisM@WorkRE: Working of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspxRun the following code, query by query. If you're still unsure, post back.[code="sql"]WITH N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rowsSELECT * FROM N1;WITH N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rowsSELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as num FROM N1;WITH N0 as (SELECT 1 as n UNION ALL SELECT 1) -- 2 rows,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) -- 4 rows,nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as num FROM N1)SELECT DATEADD(day,num, GETDATE()) FROM nums;[/code]Fri, 29 Nov 2013 07:07:30 GMTChrisM@WorkRE: Working of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspxThis function is using Tally Table approach to generate calendar date between the @startDate And @EndDateif you want to understand properly then using this post from Stefan Krzywicki "Tally Table Uses - Part I" [url]http://www.sqlservercentral.com/articles/Tally+Table/70735/[/url]Fri, 29 Nov 2013 06:21:48 GMTtwin.devilWorking of the functionhttp://www.sqlservercentral.com/Forums/Topic1518531-1291-1.aspxI have an existing function on my DB which is used for date manipulations.Please explain how this function works. [code="sql"]ALTER FUNCTION [dbo].[ExplodeDates](@startdate datetime, @enddate datetime)returns table asreturn (with N0 as (SELECT 1 as n UNION ALL SELECT 1),N1 as (SELECT 1 as n FROM N0 t1, N0 t2),N2 as (SELECT 1 as n FROM N1 t1, N1 t2),N3 as (SELECT 1 as n FROM N2 t1, N2 t2),N4 as (SELECT 1 as n FROM N3 t1, N3 t2),N5 as (SELECT 1 as n FROM N4 t1, N4 t2),N6 as (SELECT 1 as n FROM N5 t1, N5 t2),nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)SELECT DATEADD(day,num-1,@startdate) as thedateFROM numsWHERE num <= DATEDIFF(day,@startdate,@enddate) + 1[/code]Fri, 29 Nov 2013 06:08:44 GMTJunglee_George