Generate dates within a range in Snowflake

Viewed 464

I have a table in postgres one of the columns is year which is calculated as :

date_part('year',current_date) + generate_series(-2,4)

so if I take today's date , the output will be :

year
2021
2025
2019
2024
2023
2022
2020

How to achieve the above in snowflake environment.

Thanks in advance

2 Answers

Using GENERATOR and DATEADD:

SELECT DATEADD(year, (ROW_NUMBER() OVER(ORDER BY seq8())-3, current_date) AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));

And year only:

SELECT YEAR(CURRENT_DATE) + (ROW_NUMBER() OVER(ORDER BY seq8())-3 AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));

Below snowflake community article will give more in depth info about using functions like SEQ1(),SEQ2(),SEQ4(),SEQ8() and generator functions.

Generate gap free seq and dates

Related