Logo

SQL Server SeriesDate Function

Updated 2023-10-18 19:47:05.467000

Description

Use the table-valued function SeriesDate to generate a series of date values.

Syntax

SELECT * FROM [westclintech].[wct].[SeriesDate] (
  <@StartDate, datetime,>
 ,<@StopDate, datetime,>
 ,<@StepDays, float,>
 ,<@MaxIterations, float,>
 ,<@SeriesType, nvarchar(4000),>)

Arguments

@StartDate

the first date in the date range. The first value may be the maximum or the minimum value, depending on the @StepDays. @StartDate must be of the type datetime or of a type that implicitly converts to datetime.

@StopDate

the last value in the date range. The last value may be the maximum or the minimum value, depending on the @StepDays. @StopDate must be of the type datetime or of a type that implicitly converts to datetime.

@StepDays

the increment (or decrement) used to determine the next value in a linear series (see @SeriesType). In a random series, this is the multiple used to calculate a random date between the @StartDate and the @StopDate. @StepDays must be of the type float or of a type that implicitly converts to float.

@MaxIterations

the number of results to return. For linear series (see @SeriesType), a value of NULL will return all the values between the @StartDate and the @StopDate specified by the @StepDays. For random series, NULL is not permitted. @MaxIterations must be of the type float or of a type that implicitly converts to float. Non-integer values are truncated to integer.

@SeriesType

the letter 'L' or 'l' for linear series, or the letter 'R' or 'r' for random series. A linear series will generate numbers from the @StartDate incremented by the @StepDays until either the number of iterations is equal to @MaxIterations or the greatest value less than or equal to the @StopDate has been reached. A random series will randomly generate number that are multiples of @StepDays until @MaxIterations is reached.

Return Type

table

colNamecolDatatypecolDesc
SeqintA monotonically increasing sequence number
SeriesValuedatetimeThe date variable

Remarks

To generate a series for integer values use the SeriesInt function.

To generate a series for floating point numbers use the SeriesFloat function.

If the @StepDate > @StopDate and @SeriesType = 'L', then only one row will be returned with SeriesValue equal to @StartDate.

If the @StepDate > @StopDate and @SeriesType = 'R', then @MaxIterations rows will be returned with SeriesValue equal to the lesser of @StartDate and @StopDate.

If @SeriesType is NULL, then @SeriesType is set to 'L'.

If @StopDate is NULL, it will be calculated from @StartDate, @MaxIterations and @StepDays.

If @StepDays is NULL and @SeriesType is 'L' and @StopDate > @StartDate then @StepDays is set to 1.

If @StepDays is NULL and @SeriesType is 'L' and @StopDate < @StartDate then @StepDays is set to -1.

If @SeriesType is 'L' and SIGN(@StepDays) <> SIGN(@StopDate - @StartDate) then only one row will be returned.

If @SeriesType is 'R', @MaxIterations must be greater than 0 and not equal to NULL.

If @SeriesType is 'R', @StartDate cannot be NULL.

If @SeriesType is 'R', @StopDate cannot be NULL.

Examples

Generate a list of dates from 5/15/2010 to 6/14/2010.

SELECT *
FROM wct.SeriesDate('05/15/2010', '06/15/2010', 1, NULL, 'L');

Here is the resultant table:

SeqSeriesValue
12010-05-15 00:00:00.000
22010-05-16 00:00:00.000
32010-05-17 00:00:00.000
42010-05-18 00:00:00.000
52010-05-19 00:00:00.000
62010-05-20 00:00:00.000
72010-05-21 00:00:00.000
82010-05-22 00:00:00.000
92010-05-23 00:00:00.000
102010-05-24 00:00:00.000
112010-05-25 00:00:00.000
122010-05-26 00:00:00.000
132010-05-27 00:00:00.000
142010-05-28 00:00:00.000
152010-05-29 00:00:00.000
162010-05-30 00:00:00.000
172010-05-31 00:00:00.000
182010-06-01 00:00:00.000
192010-06-02 00:00:00.000
202010-06-03 00:00:00.000
212010-06-04 00:00:00.000
222010-06-05 00:00:00.000
232010-06-06 00:00:00.000
242010-06-07 00:00:00.000
252010-06-08 00:00:00.000
262010-06-09 00:00:00.000
272010-06-10 00:00:00.000
282010-06-11 00:00:00.000
292010-06-12 00:00:00.000
302010-06-13 00:00:00.000
312010-06-14 00:00:00.000
322010-06-15 00:00:00.000

Generate a list of times in 15 minute increments for 5/15/2010.

SELECT *
FROM wct.SeriesDate('05/15/2010', '05/16/2010', 1.0000 / 96.0000, NULL, 'L');

Here's the resultant table.

SeqSeriesValue
12010-05-15 00:00:00.000
22010-05-15 00:15:00.000
32010-05-15 00:30:00.000
42010-05-15 00:45:00.000
52010-05-15 01:00:00.000
62010-05-15 01:15:00.000
72010-05-15 01:30:00.000
82010-05-15 01:45:00.000
92010-05-15 02:00:00.000
102010-05-15 02:15:00.000
112010-05-15 02:30:00.000
122010-05-15 02:45:00.000
132010-05-15 03:00:00.000
142010-05-15 03:15:00.000
152010-05-15 03:30:00.000
162010-05-15 03:45:00.000
172010-05-15 04:00:00.000
182010-05-15 04:15:00.000
192010-05-15 04:30:00.000
202010-05-15 04:45:00.000
212010-05-15 05:00:00.000
222010-05-15 05:15:00.000
232010-05-15 05:30:00.000
242010-05-15 05:45:00.000
252010-05-15 06:00:00.000
262010-05-15 06:15:00.000
272010-05-15 06:30:00.000
282010-05-15 06:45:00.000
292010-05-15 07:00:00.000
302010-05-15 07:15:00.000
312010-05-15 07:30:00.000
322010-05-15 07:45:00.000
332010-05-15 08:00:00.000
342010-05-15 08:15:00.000
352010-05-15 08:30:00.000
362010-05-15 08:45:00.000
372010-05-15 09:00:00.000
382010-05-15 09:15:00.000
392010-05-15 09:30:00.000
402010-05-15 09:45:00.000
412010-05-15 10:00:00.000
422010-05-15 10:15:00.000
432010-05-15 10:30:00.000
442010-05-15 10:45:00.000
452010-05-15 11:00:00.000
462010-05-15 11:15:00.000
472010-05-15 11:30:00.000
482010-05-15 11:45:00.000
492010-05-15 12:00:00.000
502010-05-15 12:15:00.000
512010-05-15 12:30:00.000
522010-05-15 12:45:00.000
532010-05-15 13:00:00.000
542010-05-15 13:15:00.000
552010-05-15 13:30:00.000
562010-05-15 13:45:00.000
572010-05-15 14:00:00.000
582010-05-15 14:15:00.000
592010-05-15 14:30:00.000
602010-05-15 14:45:00.000
612010-05-15 15:00:00.000
622010-05-15 15:15:00.000
632010-05-15 15:30:00.000
642010-05-15 15:45:00.000
652010-05-15 16:00:00.000
662010-05-15 16:15:00.000
672010-05-15 16:30:00.000
682010-05-15 16:45:00.000
692010-05-15 17:00:00.000
702010-05-15 17:15:00.000
712010-05-15 17:30:00.000
722010-05-15 17:45:00.000
732010-05-15 18:00:00.000
742010-05-15 18:15:00.000
752010-05-15 18:30:00.000
762010-05-15 18:45:00.000
772010-05-15 19:00:00.000
782010-05-15 19:15:00.000
792010-05-15 19:30:00.000
802010-05-15 19:45:00.000
812010-05-15 20:00:00.000
822010-05-15 20:15:00.000
832010-05-15 20:30:00.000
842010-05-15 20:45:00.000
852010-05-15 21:00:00.000
862010-05-15 21:15:00.000
872010-05-15 21:30:00.000
882010-05-15 21:45:00.000
892010-05-15 22:00:00.000
902010-05-15 22:15:00.000
912010-05-15 22:30:00.000
922010-05-15 22:45:00.000
932010-05-15 23:00:00.000
942010-05-15 23:15:00.000
952010-05-15 23:30:00.000
962010-05-15 23:45:00.000
972010-05-16 00:00:00.000

Let's say we had a table with all the dates that the S&P 500 index was traded in 2009 and we wanted to find all the days that it was not traded on that were not a Saturday or a Sunday. We could enter something like this.

SELECT seriesvalue
FROM wct.SeriesDate('1/1/2009', '12/31/2009', 1, NULL, 'L')
WHERE datepart(dw, seriesvalue) <> 7
      and datepart(dw, seriesvalue) <> 1
EXCEPT
SELECT Date
FROM SANDP;

This returns the following result.

seriesvalue
2009-01-01 00:00:00.000
2009-01-19 00:00:00.000
2009-02-16 00:00:00.000
2009-04-10 00:00:00.000
2009-05-25 00:00:00.000
2009-07-03 00:00:00.000
2009-09-07 00:00:00.000
2009-11-26 00:00:00.000
2009-12-25 00:00:00.000

To randomly generate 20 dates between 1/1/2008 and and 12/31/2011, we would enter the following statement.

SELECT *
FROM wct.SeriesDate('1/1/2008', '12/31/2011', 1, 20, 'R');

This returns the following result.

SeqSeriesValue
12009-07-01 00:00:00.000
22008-12-14 00:00:00.000
32011-07-07 00:00:00.000
42011-12-25 00:00:00.000
52009-09-02 00:00:00.000
62009-01-11 00:00:00.000
72010-08-18 00:00:00.000
82011-04-02 00:00:00.000
92009-11-23 00:00:00.000
102010-05-16 00:00:00.000
112009-08-05 00:00:00.000
122010-07-04 00:00:00.000
132008-04-01 00:00:00.000
142010-04-25 00:00:00.000
152011-11-16 00:00:00.000
162010-09-03 00:00:00.000
172009-06-03 00:00:00.000
182008-07-27 00:00:00.000
192011-07-04 00:00:00.000
202011-04-19 00:00:00.000

If we wanted the same query to only return dates that were not weekend dates, we could enter the following statement.

SELECT TOP 20
       seq,
       seriesvalue
FROM wct.SeriesDate('1/1/2008', '12/31/2011', 1, 50, 'R')
WHERE datepart(dw, seriesvalue) <> 7
      and datepart(dw, seriesvalue) <> 1;

This produces the following result.

seqseriesvalue
12009-12-17 00:00:00.000
22008-09-19 00:00:00.000
42010-04-22 00:00:00.000
52008-07-17 00:00:00.000
62011-11-02 00:00:00.000
82009-12-09 00:00:00.000
92011-06-30 00:00:00.000
102008-10-14 00:00:00.000
122010-06-15 00:00:00.000
142011-01-31 00:00:00.000
162009-02-13 00:00:00.000
182008-04-15 00:00:00.000
192008-01-03 00:00:00.000
202011-05-10 00:00:00.000
212008-03-25 00:00:00.000
232008-09-22 00:00:00.000
252010-01-12 00:00:00.000
262008-05-02 00:00:00.000
302011-10-21 00:00:00.000
312010-05-28 00:00:00.000