Skip to content

Latest commit

 

History

History
156 lines (116 loc) · 4.68 KB

generate-series-transact-sql.md

File metadata and controls

156 lines (116 loc) · 4.68 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
GENERATE_SERIES (Transact-SQL)
Generates a series of numbers within a given interval. The interval and the step between series values are defined by the user.
kendalvandyke
kendalv
randolphwest
10/27/2023
sql
t-sql
reference
GENERATE_SERIES
GENERATE_SERIES_TSQL
GENERATE_SERIES function
analytic functions, GENERATE_SERIES
TSQL
=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current||=fabric

GENERATE_SERIES (Transact-SQL)

[!INCLUDE sqlserver2022-asdb-asmi-fabricse-fabricdw]

Generates a series of numbers within a given interval. The interval and the step between series values are defined by the user.

::: moniker range="=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"

Compatibility level 160

GENERATE_SERIES requires the compatibility level to be at least 160. When the compatibility level is less than 160, the [!INCLUDE ssde-md] is unable to find the GENERATE_SERIES function.

To change the compatibility level of a database, refer to View or change the compatibility level of a database. ::: moniker-end

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

GENERATE_SERIES ( start , stop [ , step ] )

Arguments

start

The first value in the interval. start is specified as a variable, a literal, or a scalar expression of type tinyint, smallint, int, bigint, decimal, or numeric.

stop

The last value in the interval. stop is specified as a variable, a literal, or a scalar expression of type tinyint, smallint, int, bigint, decimal, or numeric. The series stops once the last generated step value exceeds the stop value.

The data type for stop must match the data type for start.

[ step ]

Indicates the number of values to increment or decrement between steps in the series. step is an expression of type tinyint, smallint, int, bigint, decimal, or numeric. step can be either negative or positive, but can't be zero (0).

This argument is optional. The default value for step is 1 if start is less than stop, otherwise, the default value is -1 if start is greater than stop.

If start is less than stop and a negative value is specified for step, or if start is greater than stop and a positive value is specified for step, an empty result set is returned.

Return types

Returns a single-column table containing a sequence of values in which each differs from the preceding by step. The name of the column is value. The output is the same type as start and stop.

Permissions

No permissions are required for GENERATE_SERIES. However, the user needs EXECUTE permission on the database, and permission to query any data that is used as inputs.

Examples

The following examples demonstrate the syntax for calling GENERATE_SERIES.

A. Generate a series of integer values between 1 and 10 in increments of 1 (default)

SELECT value
FROM GENERATE_SERIES(1, 10);

[!INCLUDE ssresult-md]

value
-----------
1
2
3
4
5
6
7
8
9
10

B. Generate a series of integer values between 1 and 50 in increments of 5

SELECT value
FROM GENERATE_SERIES(1, 50, 5);

[!INCLUDE ssresult-md]

value
-----------
1
6
11
16
21
26
31
36
41
46

C. Generate a series of decimal values between 0.0 and 1.0 in increments of 0.1

DECLARE @start decimal(2, 1) = 0.0;
DECLARE @stop decimal(2, 1) = 1.0;
DECLARE @step decimal(2, 1) = 0.1;

SELECT value
FROM GENERATE_SERIES(@start, @stop, @step);

[!INCLUDE ssresult-md]

value
---------------------------------------
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

Related content